cancel
Showing results for 
Search instead for 
Did you mean: 

In magento2.2.6 how can i decrease the quantity of product in cart using REST API

SOLVED

In magento2.2.6 how can i decrease the quantity of product in cart using REST API

In magento2.2.6 i am adding products to cart using REST API. i am increasing the quantity of products its working fine but not able to decrease the quantity of products. so, what should i do for decrease quantity of product in the cart.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: In magento2.2.6 how can i decrease the quantity of product in cart using REST API

Hello @pratap_penmetsa,

 

There appears to be no way to update multiple items in a cart in one request. Despite the way the cartItem body is structured as an object, it seems to only allow updating one item at a time.

 

There are two ways to do it, but neither support mass updates as far as I can see.

 

Way #1:

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

Body:

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

Note: With the PUT it seems, if you have the :itemId in the URL, you don't seem to need to SKU or item_id in the cartItem body. You still need the quote_id though.

 

Way #2:

POST V1/carts/:cartId/items

Body:

{
  "cartItem": {
    "item_id": :item_id, // this will overwrite qty
    //"sku": :my_sku, // this will increment qty
    "qty": :qty, 
    "quote_id": :cart_id
  }
}


It appears, in my testing, that the "qty" does indeed overwrite the quantity, and does not increment or add to existing quantity, if you are using item_id in both the PUT and POST versions.

 

However, if you use the POST version, and use the SKU instead of item_id, then it will add new items to the cart, effectively "incrementing" the quantity of that product. So watch out for that.

 

--
If my answer is useful, please Accept as Solution & give Kudos

View solution in original post

2 REPLIES 2

Re: In magento2.2.6 how can i decrease the quantity of product in cart using REST API

Hello @pratap_penmetsa,

 

There appears to be no way to update multiple items in a cart in one request. Despite the way the cartItem body is structured as an object, it seems to only allow updating one item at a time.

 

There are two ways to do it, but neither support mass updates as far as I can see.

 

Way #1:

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

Body:

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

Note: With the PUT it seems, if you have the :itemId in the URL, you don't seem to need to SKU or item_id in the cartItem body. You still need the quote_id though.

 

Way #2:

POST V1/carts/:cartId/items

Body:

{
  "cartItem": {
    "item_id": :item_id, // this will overwrite qty
    //"sku": :my_sku, // this will increment qty
    "qty": :qty, 
    "quote_id": :cart_id
  }
}


It appears, in my testing, that the "qty" does indeed overwrite the quantity, and does not increment or add to existing quantity, if you are using item_id in both the PUT and POST versions.

 

However, if you use the POST version, and use the SKU instead of item_id, then it will add new items to the cart, effectively "incrementing" the quantity of that product. So watch out for that.

 

--
If my answer is useful, please Accept as Solution & give Kudos

Re: In magento2.2.6 how can i decrease the quantity of product in cart using REST API

Thanks @gelanivishal