0

I would like to customise the error messages displayed in my GraphQL queries for a coupon code in m2.

You should implement the \GraphQL\Error\ClientAware interface to handle errors in your module that are directly related to a GraphQL field having an anticipated exception. - Magento Documentation

{
    class ApplyCouponToCart implements ResolverInterface

/**
 * @var CouponManagementInterface
 */
private $couponManagement;

/**
 * @param GetCartForUser $getCartForUser
 * @param CouponManagementInterface $couponManagement
 */
public function __construct(
    GetCartForUser $getCartForUser,
    CouponManagementInterface $couponManagement
) {
    $this->getCartForUser = $getCartForUser;
    $this->couponManagement = $couponManagement;
}

/**
 * @inheritdoc
 */
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
    if (empty($args['input']['cart_id'])) {
        throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
    }
    $maskedCartId = $args['input']['cart_id'];

    if (empty($args['input']['coupon_code'])) {
        throw new GraphQlInputException(__('Required parameter "coupon_code" is missing'));
    }
    $couponCode = $args['input']['coupon_code'];

    $currentUserId = $context->getUserId();
    $storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
    $cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
    $cartId = $cart->getId();

    /* Check current cart does not have coupon code applied */
    $appliedCouponCode = $this->couponManagement->get($cartId);
    if (!empty($appliedCouponCode)) {
        throw new GraphQlInputException(
            __('A coupon is already applied to the cart. Please remove it to apply another')
        );
    }

I need an example of how I can catch this exceptions below, and customize just the text errors like

    throw new GraphQlInputException(
        __('I want to customise this text ')

enter image description here

1
  • if the text that dosn't have any variable you can add translation in your i18n file this is best and simple way to change Message
    – Waqar Ali
    Commented Oct 15, 2021 at 13:04

1 Answer 1

1

if the text that dosn't have any variable you can add translation in your i18n file this is best and simple way to change Message

in you app/i18n/en_US.csv file

"A coupon is already applied to the cart. Please remove it to apply another","I want to customise this text",module,Magento_QuoteGraphQl

5
  • Thanks but "app/i18n/en_US.csv" is a Magento 1 feature, I'm using Magento 2.4
    – Kbee
    Commented Oct 18, 2021 at 8:03
  • its same in magento 2
    – Waqar Ali
    Commented Oct 18, 2021 at 11:09
  • You're correct found it in "lib/web/i18n/en_US.csv" . After testing on Postman, I found the error messages were from the magento/module-quote-graph-ql/Model/Resolver/ApplyCouponToCart.php, this is where I want to update the messages.
    – Kbee
    Commented Oct 18, 2021 at 14:38
  • can you please show actual message?
    – Waqar Ali
    Commented Oct 18, 2021 at 14:39
  • Updated above with image.
    – Kbee
    Commented Oct 19, 2021 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.