GraphQL checkout tutorial

Step 7. Apply a coupon

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 “Get customer authorization token” 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_coupon {
        code
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
{
  "data": {
    "applyCouponToCart": {
      "cart": {
        "applied_coupon": {
          "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 “Get customer authorization token” for more information.

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

Response:

1
2
3
4
5
6
7
8
9
10
11
{
  "data": {
    "removeCouponFromCart": {
      "cart": {
        "applied_coupon": {
          "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.