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)

4 Answers 4

2

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.)

1
  • 1
    its adding new item to cart, not Changing Quantity Jun 3, 2019 at 11:29
1

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)

1
  • 1
    still on it mate...
    – apedic
    May 19, 2022 at 13:50
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.

1
  • its adding new item to cart, not Changing Quantity Jun 3, 2019 at 11:29
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"
  }
}
3
  • 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, 2016 at 18:39
  • 1
    Do you find a native solution for this ? Mar 27, 2017 at 14:44
  • 1
    Do you find a solution ?
    – Jsparo30
    Apr 22, 2017 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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