Skip to content

Conversation

@vedanshujain
Copy link
Contributor

@vedanshujain vedanshujain commented Dec 17, 2020

All Submissions:

Changes proposed in this Pull Request:

Stock managment fixes:

  1. Use $already_reduced_stock instead of also considering $refunded_item_quantity while 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.

  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.

Closes #28605 #28505 #28585 . Reopen #27504 for more consideration

How to test the changes in this Pull Request:

  1. Follow steps in Manually deleting product from an order no longer adjusts stock #28605 Stock reduced when 'Pending Payment' order is set to 'Cancelled' #28505 and Stock is decreased when you hit Update for already Cancelled order #28585

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changelog entry

Fix - Adjust stock items only for statuses which reduces the stock.

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.
@vedanshujain vedanshujain marked this pull request as ready for review December 17, 2020 17:06
@vedanshujain vedanshujain requested review from a team, peterfabian and roykho and removed request for a team December 17, 2020 17:06
roykho
roykho previously approved these changes Dec 17, 2020
Copy link
Contributor

@roykho roykho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@peterfabian peterfabian left a 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:

  1. create order from the front end---ok
  2. reduce stock from wp admin by edit button---ok
  3. increase stock from wp admin by the edit button---ok
  4. refund some stock---ok
  5. delete line item---ok
  6. add the line item back---stock level not ok
  7. new order from front end---ok
  8. refund 2 items---ok
  9. Cancel the order---ok
  10. back to On hold---not ok (it decreases based on the full stock amount, not the refunded amount)
  11. editing qty of item in the order---ok (fixes the overall count to correct count, i.e. fixes the problem in the previous step)
  12. removing line item---ok

Maybe we should update the notice when deleting an item from an order, too?
Screenshot 2020-12-18 at 14 41 34

$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() );
Copy link
Contributor

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.

Copy link
Contributor Author

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 );
Copy link
Contributor

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?

Copy link
Contributor Author

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',
Copy link
Contributor

@peterfabian peterfabian Dec 18, 2020

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.

@vedanshujain
Copy link
Contributor Author

@peterfabian

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)

This is expected, we removed stock changes unless status is also changed to prevent ghost stock issues. See #26642.

reinstantiating an order with refunds

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:

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.

@vedanshujain vedanshujain added the release: highlight Issues that have a high user impact and need to be discussed/paid attention to. label Dec 21, 2020
Copy link
Contributor

@claudiosanches claudiosanches left a 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.

@vedanshujain vedanshujain dismissed peterfabian’s stale review December 21, 2020 17:06

addressed feedback comments

@vedanshujain
Copy link
Contributor Author

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.

@vedanshujain vedanshujain merged commit 800c6a6 into master Dec 21, 2020
@vedanshujain vedanshujain deleted the fix/28505 branch December 21, 2020 17:06
@woocommercebot woocommercebot added release: add changelog Mark all PRs that have not had their changelog entries added. [auto] release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] labels Dec 21, 2020
@vedanshujain vedanshujain removed the release: add changelog Mark all PRs that have not had their changelog entries added. [auto] label Dec 21, 2020
@vedanshujain vedanshujain added this to the 4.9.0 milestone Dec 21, 2020
@juliaamosova juliaamosova added testing instructions added and removed release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] labels Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: highlight Issues that have a high user impact and need to be discussed/paid attention to.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Manually deleting product from an order no longer adjusts stock

7 participants