Skip to content

fix(sales): skip stock validation when editing order with fully consumed stock#40664

Open
lbajsarowicz wants to merge 3 commits intomagento:2.4-developfrom
lbajsarowicz:fix/39898-order-edit-out-of-stock
Open

fix(sales): skip stock validation when editing order with fully consumed stock#40664
lbajsarowicz wants to merge 3 commits intomagento:2.4-developfrom
lbajsarowicz:fix/39898-order-edit-out-of-stock

Conversation

@lbajsarowicz
Copy link
Copy Markdown
Contributor

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 via addProduct(), which triggers QuantityValidator stock 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 IsSuperMode on the quote during the initFromOrder() item loading loop. The QuantityValidator explicitly skips validation when super mode is active (line 135-137):

if ($quoteItem->getQuote()->getIsSuperMode()) {
    return;
}

This is safe because:

  • The stock was already allocated when the original order was placed
  • The original order is canceled after the new one is submitted (releasing stock)
  • Super mode is restored to its previous value after the loop

Fixes #39898

Files Changed

  • app/code/Magento/Sales/Model/AdminOrder/Create.php — enable super mode during initFromOrder() item loading

Manual Testing Scenarios

  1. Create a product with stock qty = 100
  2. Place an order for 100 units (stock now fully consumed)
  3. Go to Admin > Sales > Orders, open the order, click Edit
  4. Expected: Order edit page loads successfully with all items
  5. Before fix: Error "Product is out of stock" displayed

⭐ 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

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
@m2-assistant
Copy link
Copy Markdown

m2-assistant bot commented Apr 9, 2026

Hi @lbajsarowicz. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@lbajsarowicz
Copy link
Copy Markdown
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
@lbajsarowicz
Copy link
Copy Markdown
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
@lbajsarowicz
Copy link
Copy Markdown
Contributor Author

@magento run all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error appears as 'Product is out of stock' when editing orders in the backend without MSI.

1 participant