-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Stock management regression fixes #28620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This loops and tests stock changes through ALL supported WooCommerce statuses. Hopefully this will make our inventory tracking more robust.
1. Use $already_reduced_stock instead of also considering $refunded_item_quantity while deleting orders. This will bring back part of #27504 again, but for now this seems to be the best solution for countering #28605. It needs discussion whether deleting a line item completely should also undo any refund related changes on it or not. 2. Also mark `stock_reduced` flag on order if any of the line item has any `_reduced_stock` flag. This will allow for stock restoring logic to work properly when order is cancelled. 3. Only adjust line item stock when order is in `processing`, `completed` or `on-hold` status state, because we need to reduce stocks on these status only. Stock adjustments in refunds or when changing statuses is already taken care of by their specific hooks.
roykho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With these new changes, are the older unit tests still needed? https://github.com/woocommerce/woocommerce/blob/master/tests/php/includes/admin/class-wc-admin-functions-test.php#L72-L288
Otherwise your PR tested well.
peterfabian
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, mostly it seems to be working fine, I just found a couple of issues/questions:
I'm not sure if this is in scope, but these cases seems to not work for me as I would expect:
- from wp admin, adding a product to an order does not reduce the stock (and any subsequent changes also do nothing to the stock level for me)
- reinstantiating an order with refunds
Complete list of what I tested:
- create order from the front end---ok
- reduce stock from wp admin by edit button---ok
- increase stock from wp admin by the edit button---ok
- refund some stock---ok
- delete line item---ok
- add the line item back---stock level not ok
- new order from front end---ok
- refund 2 items---ok
- Cancel the order---ok
- back to On hold---not ok (it decreases based on the full stock amount, not the refunded amount)
- editing qty of item in the order---ok (fixes the overall count to correct count, i.e. fixes the problem in the previous step)
- removing line item---ok
Maybe we should update the notice when deleting an item from an order, too?

| $this->assertEquals( $before_transition, $product->get_stock_quantity() ); | ||
|
|
||
| // Changing status from UI also calls this method in metadata save hook. This has impact on stock levels, so simulate it here as well. | ||
| wc_save_order_items( $order, $order->get_items() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that you noticed this.
General remark: I wonder if this is somewhat fragile for a test, as we might miss something like this easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I missed this initially but only noticed it when tests were passing but there was clearly a bug while doing the same steps via interface. Adding this made the tests fail when I was expecting them to, so I agree that code around these parts is bit fragile, and is a good candidate for refactoring efforts.
| public function test_status_transition_stock_reduce_to_stock_reduce() { | ||
| foreach ( $this->order_stock_reduce_statuses as $order_status_from ) { | ||
| foreach ( $this->order_stock_reduce_statuses as $order_status_to ) { | ||
| $this->transition_order_status_and_assert_stock_quantity( $order_status_from, $order_status_to, 9, 9 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm I understand, this is supposed to test that the stock isn't reduced twice, right? Do you think it would make sense to add it to the assert message to be clear about the intent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like adding it in assert messages is a bit confusing because assert can fail whenever expected and actual stock level would not match, which can happen for a lot of reason than just stock being reduced twice (for example, there could be a bug which increases the stock, and assert message will say stock is not reduced twice).
But I agree that its a bit unclear at the moment, so I have added expectations in doc comments for all these tests in 5171108. Let me know if these makes sense.
| * @var array List of statuses which have no impact on inventory. | ||
| */ | ||
| public $order_stock_no_effect_statuses = array( | ||
| 'wc-failed', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should failed not restore stock eventually? I'm just not sure the assumptions on all those statuses are aligned.
Checking https://docs.woocommerce.com/document/managing-orders/ it seems that order can be in failed status also because
The hold stock window expired without a response
So I wonder if there isn't any potential problem there. But probably not, since failed comes after pending in the diagram and it does not reduce the stock level, only puts the stock on hold, so there is nothing to restore. Okay, nevermind :) I'll leave this here just in case someone would wonder about the same thing.
This is expected, we removed stock changes unless status is also changed to prevent ghost stock issues. See #26642.
This is also kinda expected, because as part of this PR I am reverting some of the related changes to have more discussion. See from the PR description:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be working as expected, I followed all steps to reproduce and I agree with your comments in #28620 (comment)
I'm not merging now because it's still missing unit test to run on Travis (in localhost those works fine) after I fixed some coding standards.
addressed feedback comments
|
Merging because manually tested the failed e2e tests (able to delete refund). there is another one about setup wizard failing, but it does not looks related. |
All Submissions:
Changes proposed in this Pull Request:
Stock managment fixes:
Use
$already_reduced_stockinstead of also considering$refunded_item_quantitywhile deleting orders. This will bring back part of Stock quantity restocked twice when deleting Order line item after refund #27504 again, but for now this seems to be the best solution for countering Manually deleting product from an order no longer adjusts stock #28605. It needs discussion whether deleting a line item completely should also undo any refund related changes on it or not.Also mark
stock_reducedflag on order if any of the line item has any_reduced_stockflag. This will allow for stock restoring logic to work properly when order is cancelled.Only adjust line item stock when order is in
processing,completedoron-holdstatus state, because we need to reduce stocks on these status only. Stock adjustments in refunds or when changing statuses is already taken care of by their specific hooks.Closes #28605 #28505 #28585 . Reopen #27504 for more consideration
How to test the changes in this Pull Request:
Other information:
Changelog entry