applyCouponToCart mutation

The applyCouponToCart mutation applies a pre-defined coupon code to the specified cart. Valid coupon codes are defined in cart price rules.

Syntax

mutation: {applyCouponToCart(input: ApplyCouponToCartInput) {ApplyCouponToCartOutput}}

Example usage

The following example applies the coupon code H2O to the cart. For this coupon to be valid, the Affirm Water Bottle (sku: 24-UG06) must be in the cart.

Request:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mutation {
  applyCouponToCart(
    input: {
      cart_id: "IeTUiU0oCXjm0uRqGCOuhQ2AuQatogjG",
      coupon_code: "H20"
    }
  ) {
    cart {
      items {
        product {
          name
        }
        quantity
      }
      applied_coupons {
        code
      }
      prices {
        grand_total{
          value
          currency
        }
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
  "data": {
    "applyCouponToCart": {
      "cart": {
        "items": [
          {
            "product": {
              "name": "Gold Membership"
            },
            "quantity": 2
          },
          {
            "product": {
              "name": "Strive Shoulder Pack"
            },
            "quantity": 1
          },
          {
            "product": {
              "name": "Affirm Water Bottle "
            },
            "quantity": 1
          }
        ],
        "applied_coupons": {
          "code": "H20"
        },
        "prices": {
          "grand_total": {
            "value": 134.08,
            "currency": "USD"
          }
        }
      }
    }
  }
}

Input attributes

The applyCouponToCart mutation requires the cart_id and coupon_code.

ApplyCouponToCartInput object

The ApplyCouponToCartInput object must contain the following attributes:

Attribute Data Type Description
cart_id String! The unique ID that identifies the customer’s cart
coupon_code String! A valid coupon code

Output attributes

The ApplyCouponToCartOutput object contains the Cart object.

Attribute Data Type Description
cart Cart! Describes the contents of the specified shopping cart

Cart object

Attribute Data Type Description
applied_coupon AppliedCoupon Deprecated. Use applied_coupons instead
applied_coupons [AppliedCoupon] An array of AppliedCoupon objects. Each object contains the code text attribute, which specifies the coupon code
applied_gift_cards [AppliedGiftCard] An array of AppliedGiftCard objects. An AppliedGiftCard object contains the code text attribute, which specifies the gift card code. applied_gift_cards is a Commerce-only attribute, defined in the GiftCardAccountGraphQl module
applied_reward_points RewardPointsAmount The amount of reward points applied to the cart
applied_store_credit AppliedStoreCredit Contains store credit information applied to the cart. applied_store_credit is a Commerce-only attribute, defined in the CustomerBalanceGraphQl module
available_gift_wrappings [GiftWrapping]! The list of available gift wrapping options for the cart
available_payment_methods [AvailablePaymentMethod] Available payment methods
billing_address BillingCartAddress Contains the billing address specified in the customer’s cart
email String The customer’s email address
gift_message GiftMessage A gift message added to the cart
gift_receipt_included Boolean! Indicates if the customer requested a gift receipt for the cart
gift_wrapping GiftWrapping The selected gift wrapping for the cart
id ID! The ID of the cart
is_virtual Boolean! Indicates whether the cart contains only virtual products
items [CartItemInterface] Contains the items in the customer’s cart
prices CartPrices Contains subtotals and totals
printed_card_included Boolean! Indicates if the customer requested a printed card for the cart
selected_payment_method SelectedPaymentMethod Selected payment method
shipping_addresses [ShippingCartAddress]! Contains one or more shipping addresses
total_quantity Float! Total Quantity of products in the cart

Cart query output provides more information about the Cart object.

Errors

Error Description
A coupon is already applied to the cart. Please remove it to apply another The value specified in the coupon_code argument has already applied to cart. Use removeCouponFromCart to remove the current coupon and to apply another.
Cart does not contain products. The coupon cannot be applied to an empty cart.
Could not find a cart with ID "XXX" The specified cart_id value does not exist in the quote_id_mask table.
Required parameter "coupon_code" is missing The required coupon_code argument contains an empty value.
The coupon code isn't valid. Verify the code and try again. The entered coupon code is not applicable. Check the existing shopping cart price rules for details.
The current user cannot perform operations on cart XXX An unauthorized user (guest) tried to add the product into a customer’s cart, or an authorized user (customer) tried to add the product into the cart of another customer.