Magento 2 How to createEmptyCart, updateCartItems and removeCartItems using GraphQL

We all know that added cart items are stored in the quote table, here we will show you how to update cart items, remove cart items and how to create an empty cart using GraphQL in Magento 2.

Create an empty cart

The createEmptyCart mutation allows create an empty cart for a guest or logged in customer, must include the customer's authorization token in the header of the request for login customer.

Syntax

Copy
mutation {createEmptyCart}: String

The following request returns create an empty cart.

Request

Copy
mutation {
  createEmptyCart
}

Response

Copy
{
  "data": {
    "createEmptyCart": "OlljFSsHfQ4P6I4loBnjBEGJdlanD0D3"
  }
}

Update an empty cart

The updateCartItems mutation allows you to replace the current quantity of one or more cart items with the specified quantities. Note: If we will set quantity to 0 (zero) removes as item from cart.

Syntax

Copy
mutation: {
 updateCartItems(
  input: UpdateCartItemsInput
 ) {
  UpdateCartItemsOutput
 }
}

In the following query change the quantity of cart item.

Request

Copy
mutation {
  updateCartItems(
    input: {
      cart_id: "0v1WorIkx7tdcZIvymRMdPx4HaOYAXQN"
      cart_items: [{ cart_item_id: 19, quantity: 2 }]
    }
  ) {
    cart {
      items {
        id
        quantity
        product {
          id
          sku
          name
        }
        prices {
          row_total {
            value
            currency
          }
        }
      }
    }
  }
}

Response

Copy
{
  "data": {
    "updateCartItems": {
      "cart": {
        "items": [
          {
            "id": "19",
            "quantity": 2,
            "product": {
              "id": 14,
              "sku": "24-WB04",
              "name": "Push It Messenger Bag"
            },
            "prices": {
              "row_total": {
                "value": 90,
                "currency": "USD"
              }
            }
          }
        ]
      }
    }
  }
}

Remove Items from cart

The removeItemFromCart mutation deletes the entire quantity of a specific item. You can remove all items from cart.

Syntax

Copy
mutation {
  removeItemFromCart(
    input: RemoveItemFromCartInput
  ) {
    RemoveItemFromCartOutput
  }
}

The following example removes cart item from cart.

Request

Copy
mutation {
  removeItemFromCart(
    input: {
      cart_id: "0v1WorIkx7tdcZIvymRMdPx4HaOYAXQN"
      cart_item_id: 20
    }
  ) {
    cart {
      items {
        id
        quantity
        product {
          id
          sku
          name
        }
        prices {
          price{
            value
            currency
          }
        }
      }
    }
  }
}

Response

Copy
{
  "data": {
    "removeItemFromCart": {
      "cart": {
        "items": []
      }
    }
  }
}

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 Posts
    Tags
    Newsletter
    SocialFollow on Twitter
    Visit now!