3

How to change quantity of product in cart using REST API in magento 2?

How can i change quantity of product listed in cart of magento 2 using api call ? (without changing order of items)

1

How to change quantity of product in cart using REST API ?

http://magento.host/index.php/rest/V1/carts/mine/items/{item_id}
  • method : **PUT**
  • Authorization : Bearer <customer token>

BODY

{
  "cartItem": {
    "sku": "MH11",
    "qty": 1,
    "quote_id": "3",
    "product_option": {
      "extension_attributes": {
        "configurable_item_options": [
          {
            "option_id": "93",
            "option_value": 58
          },
          {
            "option_id": "141",
            "option_value": 168
          }
        ]
      }
    },
    "extension_attributes": {}
  }
}

FROM HERE WE CAN EDIT CART ITEM (Like Qty, Attribute options etc.)

  • its adding new item to cart, not Changing Quantity – Praveen Negimani Jun 3 at 11:29
0

This is working for me to update (and overwrite, not increment) the quantity of an item in a guest cart via API call:

PUT V1/guest-carts/:cartId/items/:itemId

Body:

{
  "cartItem": {
    "item_id": :item_id, 
    "qty": :qty, 
    "quote_id": :cart_id_masked
  }
}

Perhaps the way item quantities were being incremented instead of overwritten is a bug that was fixed?

If you use the POST method, with a sku instead of item_id, then the quantity if incremented, I noticed. Not not when using PUT.

  • its adding new item to cart, not Changing Quantity – Praveen Negimani Jun 3 at 11:29
0

Regarding reducing qty you have to omit the product_options whilst doing the PUT request to the endpoint (rest/V1/carts/mine/items/{item_id}):

"cartItem": { "sku": "WSH05", "qty": 5, "quote_id": "3" } }

The problem with this is that it doesn't do any error checking / handling, so you can set any amount of stock even if it isn't available:

"cartItem": { "sku": "WSH05", "qty": 101, "quote_id": "3" } }

enter image description here

I have created a github issue to see if this is intended behaviour or whether we should actually get an error message. (https://github.com/magento/magento2/issues/12846)

0

Assuming you want to change the quantity of a single item in a customer's cart, call:

POST V1/carts/mine/items

With a payload similar to

{
  "cartItem": {
    "item_id": 5,
    "qty": 4,
    "quote_id": "6"
  }
}
  • 1
    if i call this it will add 4 quantities , and i call again it will add 4 quantities. its called adding. I not only want to increase quantities. i do want to decrease also. so, what should i do for decrease quantities in the cart. – Manoj Nov 27 '16 at 18:39
  • 1
    Do you find a native solution for this ? – Franck Garnier Mar 27 '17 at 14:44
  • 1
    Do you find a solution ? – Jsparo30 Apr 22 '17 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.