-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
Preconditions
- Magento EE 2.1.9 (with sample data)
- PHP 7.0
- nginx 1.11
- 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
- Create a customer, and get their customer token from: http://localhost:8000/index.php/rest/default/V1/integration/customer/token
- Get admin token http://localhost:8000/index.php/rest/default/V1/integration/admin/token
- 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 - Get current cart_id http://localhost:8000/index.php/rest/default/V1/carts/mine
4.1 Use customer bearer token
Proper steps
- 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" } ] } } } } - 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
- Cart QTY is reduced to 10
Actual result
- 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:
