This is a beta release of documentation for Magento 2.4, published for previewing soon-to-be-released functionality. Content in this version is subject to change. Links to the v2.4 code base may not properly resolve until the code is officially released.

Step 7. Apply a coupon

GraphQL checkout tutorial

Use applyCouponToCart to apply a discount coupon to the specified cart_id.

{ CART_ID } is the unique shopping cart ID from Step 2. Create empty cart.

{ COUPON_CODE } is an existing Magento coupon code. It cannot be generated with GraphQL.

Request:

For logged-in customers, send the customer’s authorization token in the Authorization parameter of the header. See Authorization tokens for more information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mutation {
  applyCouponToCart(
    input: {
      cart_id: "{ CART_ID }"
      coupon_code: "{ COUPON_CODE }"
    }
  ) {
    cart {
      applied_coupons {
        code
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
{
  "data": {
    "applyCouponToCart": {
      "cart": {
        "applied_coupons": {
          "code": "{ COUPON_CODE }"
        }
      }
    }
  }
}

Use removeCouponFromCart to remove a discount coupon from the shopping cart.

Request:

For logged-in customers, send the customer’s authorization token in the Authorization parameter of the header. See Authorization tokens for more information.

1
2
3
4
5
6
7
8
9
mutation {
  removeCouponFromCart(input: { cart_id: "{ CART_ID }" }) {
    cart {
      applied_coupons {
        code
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
{
  "data": {
    "removeCouponFromCart": {
      "cart": {
        "applied_coupons": {
          "applied_coupon": null
        }
      }
    }
  }
}

Verify this step

  1. Sign in as a customer to the website using the email john.doe@example.com and password b1b2b3l@w+.

  2. Go to Checkout.

  3. The discount is displayed in the Order Summary block.