Skip to content

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

@jzahedieh

Description

@jzahedieh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.2.xThe issue has been fixed in 2.2 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions