Skip to content

Admin product save overwrites inventory qty even when it is unchanged #9345

Closed
@cykirsch

Description

@cykirsch

Preconditions

  1. As far as I know, this issue exists in all versions of Magento 2.x (and 1.x)

Steps to reproduce

  1. Go to the Manage Product admin page for SKU-123.
  2. Change the Qty to 10 and save. The reloaded page will show Qty 10.
  3. In a new tab, either admin or frontend, place a new order for Qty 2 of SKU-123.
  4. At this point, if you query the cataloginventory_stock_item table, the qty will be 8, because 2 out of 10 have been purchased.
  5. Back on the original tab (Manage Product admin page), without making any changes, click Save.

Expected result

  1. Because no changes were made to the qty on the admin product page, no changes should be made to the qty in the database. Therefore, when the page loads after saving, the qty field should display 8.

Actual result

  1. The exact value in the Qty field on the admin product page (in the above example, 10) is saved to the database. Therefore, when the page loads after saving, the qty field displays 10.

Further details from past experience

Magento 1 & 2 do both implement a qty_correction mechanism on the Stock\Item. Unfortunately, this only accounts for any changes made to the database since the Stock Item object was loaded. This object is obviously not loaded until the request is initiated by clicking the Save button.

Instead of accounting for how much the qty has changed since the Stock Item object was loaded, Magento should really account for how much the qty has changed since the product page was loaded.

How we fixed the issue in our M1 store

...and we will do this in our M2 store as well if Magento doesn't implement a fix

Add an additional (hidden) field to the admin product page that holds the original_inventory_qty. In the save request, we then set qty_correction = new - original, where new and original both come from the admin inputs. Magento\CatalogInventory\Model\ResourceModel\Stock\Item::_prepareDataForTable() will take it from here and change that qty_correction to a qty +/- db statement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions