Description
Preconditions
- As far as I know, this issue exists in all versions of Magento 2.x (and 1.x)
Steps to reproduce
- Go to the Manage Product admin page for SKU-123.
- Change the Qty to 10 and save. The reloaded page will show Qty 10.
- In a new tab, either admin or frontend, place a new order for Qty 2 of SKU-123.
- At this point, if you query the cataloginventory_stock_item table, the qty will be 8, because 2 out of 10 have been purchased.
- Back on the original tab (Manage Product admin page), without making any changes, click Save.
Expected result
- 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
- 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.