magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.48k stars 9.29k forks source link

API carts/mine/items update|reduce qty update throws stock error #12846

Closed jzahedieh closed 6 years ago

jzahedieh commented 6 years ago

Preconditions

  1. Magento EE 2.1.9 (with sample data)
  2. PHP 7.0
  3. nginx 1.11
  4. percona:5.7

Using https://github.com/mageinferno/magento2-docker-compose

Steps to reproduce

Precondition steps

These are for getting the data needed for the proper steps

  1. Create a customer, and get their customer token from: http://localhost:8000/index.php/rest/default/V1/integration/customer/token
  2. Get admin token http://localhost:8000/index.php/rest/default/V1/integration/admin/token
  3. Get SKU option for WSH05 (size:28, color: blue) 3.2 Use admin bearer token 3.1 http://localhost:8000/index.php/rest/V1/products/WSH05
  4. Get current cart_id http://localhost:8000/index.php/rest/default/V1/carts/mine 4.1 Use customer bearer token

Proper steps

  1. Add item to cart with qty 95 (sample data has stock level @ 100) 1.1 POST http://localhost:8000/index.php/rest/V1/carts/mine/items 1.2 Use customer bearer token 1.3 JSON BODY:{ "cartItem": { "sku": "WSH05", "qty": 95, "quote_id": "3", "product_option": { "extension_attributes": { "configurable_item_options": [ { "option_id": "93", "option_value": "50" }, { "option_id": "160", "option_value": "184" } ] } } } }
  2. Update item need item_id from current cart API query, reduce to qty of 10 2.1 PUT http://localhost:8000/index.php/rest/V1/carts/mine/items/15 2.2 Use customer bearer token 2.3 JSON BODY:{ "cartItem": { "sku": "WSH05", "qty": 10, "quote_id": "3", "product_option": { "extension_attributes": { "configurable_item_options": [ { "option_id": "93", "option_value": "50" }, { "option_id": "160", "option_value": "184" } ] } } } }

Expected result

  1. Cart QTY is reduced to 10

Actual result

  1. 400 bad request error mesage: { "message": "We don't have as many \"Bess Yoga Short\" as you requested." }

Analysis

The main problem seems to be Magento\Quote\Model\Quote\Item\Processor::prepare() method calling the item class addQty method rather than the setQty method.

addQty adds the difference to the old value $this->setQty($this->getQty() + $qty);, so from 95 to 10 it will do 95+10 which is over the available stock level thus the error being triggered.

Workaround

It is possible to modify the qty of the item using a PUT request by omitting the product_option object in the payload: { "cartItem": { "sku": "WSH05", "qty": 10, "quote_id": "3", } }

The problem with this is that there isn't any error checking so the following will be a valid request: { "cartItem": { "sku": "WSH05", "qty": 101, "quote_id": "3" } }

Which causes the following errors to display on the basket/checkout:

image

magento-engcom-team commented 6 years ago

@jzahedieh, thank you for your report. We've acknowledged the issue and added to our backlog.

magento-engcom-team commented 6 years ago

@jzahedieh The issue is already fixed in 2.2. Closing ticket for now. Please reopen if you need any further assistance with this.

Ctucker9233 commented 6 years ago

@magento-engcom-team What are the commits for this issue?

brunogemelli commented 3 years ago

I am facing a similar issue in Magento Commerce Edition - v2.3.5-p1.

@magento-engcom-team What were the commits for this fix?

apedicdev commented 2 years ago

looks like issue is back on Magento 2.3.5-p2 or maybe never really fixed