fix(sales): skip stock validation when editing order with fully consumed stock#40664
Open
lbajsarowicz wants to merge 3 commits intomagento:2.4-developfrom
Open
fix(sales): skip stock validation when editing order with fully consumed stock#40664lbajsarowicz wants to merge 3 commits intomagento:2.4-developfrom
lbajsarowicz wants to merge 3 commits intomagento:2.4-developfrom
Conversation
When editing an order that consumed all available stock, the admin order edit showed 'Product is out of stock' because Magento re-validated stock availability for items being re-added to the quote without accounting for the original order's allocation. Enable super mode on the quote during initFromOrder() item loading to bypass stock validation. This is correct because: - The stock was already allocated when the original order was placed - The original order is canceled after the new one is submitted - The same pattern is used elsewhere in admin order creation Fixes magento#39898
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
Contributor
Author
|
@magento run all tests |
New unit test verifying initFromOrder() enables IsSuperMode on the quote before re-adding items and restores it after. This ensures stock validation is skipped for items being re-added from the original order. Relates to magento#39898
Contributor
Author
|
@magento run all tests |
Verifies the end-to-end flow: create product with qty=5, place order for all 5 units (stock fully consumed), then initFromOrder() on the placed order. Asserts the edit quote contains items and no error messages are generated. Uses modern #[DataFixture] attributes with the full checkout chain: Product → ProductStock → Customer → Cart → AddToCart → Addresses → Delivery → Payment → PlaceOrder. Relates to magento#39898
Contributor
Author
|
@magento run all tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When an order consumed all available stock for a product (e.g., ordered 100 units of a 100-unit product), attempting to edit that order in the admin showed "Product is out of stock" error. The edit failed even though the stock was legitimately allocated by the original order.
Root Cause
AdminOrder\Create::initFromOrder()re-adds order items to a new quote viaaddProduct(), which triggersQuantityValidatorstock checks. Since the original order already consumed the stock,$stockItem->getIsInStock()returns false, and the validator rejects the item.The original order is canceled in
afterSubmit()(releasing the stock) only after the new order is successfully placed — but the validation happens before that.Fix
Enable
IsSuperModeon the quote during theinitFromOrder()item loading loop. TheQuantityValidatorexplicitly skips validation when super mode is active (line 135-137):This is safe because:
Fixes #39898
Files Changed
app/code/Magento/Sales/Model/AdminOrder/Create.php— enable super mode duringinitFromOrder()item loadingManual Testing Scenarios
⭐ Support my work
Do you like the fix? Remember to react with "👍🏻" to get it merged faster,
Then Sponsor me on Github so I can spend more time on fixing issues like this one.
Learn more at https://github.com/sponsors/lbajsarowicz