Magento 2 How to apply and remove coupon to cart using GraphQL

Generally, valid coupon codes are defined in cart price rules (Magento admin), and we will show you that this coupon how to apply and remove using GraphQL.

Apply coupon to cart

We use mutation applyCouponToCart for pre-defined coupon code to the specific cart.

Syntax

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

We will check the H20 coupon code to the cart in the following mutation example.

Request

saveCopyzoom_out_map
mutation {
  applyCouponToCart(
    input: {
      cart_id: "2U1u9wHHOFNR3UAnBWMTznJfyKA74ZIc",
      coupon_code: "H20"
    }
  ) {
    cart {
      items {
        product {
          sku
          name
        }
        quantity
      }
      applied_coupons {
        code
      }
      prices {
        grand_total{
          value
          currency
        }
      }
    }
  }
}

Response

saveCopyzoom_out_map
{
  "data": {
    "applyCouponToCart": {
      "cart": {
        "items": [
          {
            "product": {
              "sku": "24-WB04",
              "name": "Push It Messenger Bag"
            },
            "quantity": 1
          },
          {
            "product": {
              "sku": "24-UG06",
              "name": "Affirm Water Bottle "
            },
            "quantity": 1
          }
        ],
        "applied_coupons": [
          {
            "code": "H20"
          }
        ],
        "prices": {
          "grand_total": {
            "value": 47.1,
            "currency": "USD"
          }
        }
      }
    }
  }
}

Note: The applyCouponToCart mutation requires the cart_id and coupon_code.

Remove coupon from cart

In the above, we have shown how to apply the coupon to the cart. Now, we will show you how to remove this applied coupon code from the cart.

We use mutation removeCouponFromCart removes a previously-applied coupon from the cart.

Syntax

saveCopyzoom_out_map
mutation {
  removeCouponFromCart(
    input: RemoveCouponFromCartInput
  ) {
    RemoveCouponFromCartOutput
  }
}

In the following example, we will remove the H20 coupon from the cart.

Request

saveCopyzoom_out_map
mutation {
  removeCouponFromCart(input: { cart_id: "2U1u9wHHOFNR3UAnBWMTznJfyKA74ZIc" }) {
    cart {
      items {
        product {
          sku
          name
        }
        quantity
      }
      applied_coupons {
        code
      }
      prices {
        grand_total {
          value
          currency
        }
      }
    }
  }
}

Response

saveCopyzoom_out_map
{
  "data": {
    "removeCouponFromCart": {
      "cart": {
        "items": [
          {
            "product": {
              "sku": "24-WB04",
              "name": "Push It Messenger Bag"
            },
            "quantity": 1
          },
          {
            "product": {
              "sku": "24-UG06",
              "name": "Affirm Water Bottle "
            },
            "quantity": 1
          }
        ],
        "applied_coupons": null,
        "prices": {
          "grand_total": {
            "value": 52,
            "currency": "USD"
          }
        }
      }
    }
  }
}

Note: The cart must contain at least one item in order to remove the coupon.

That's it!!

If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.

I hope this article helps you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.




Search
Recent Articles
Tags
Newsletter
Copyright © 2022 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off