-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Fix/ Decrease product total_sales when an order is reversed #23796 #37842
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
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## trunk #37842 +/- ##
==========================================
- Coverage 51.6% 51.5% -0.0%
+ Complexity 17444 17280 -164
==========================================
Files 440 430 -10
Lines 80297 80039 -258
==========================================
- Hits 41401 41230 -171
+ Misses 38896 38809 -87
|
|
Thanks for working on this, @Abdalsalaam. However, on further review, we're not sure it makes sense to decrease this value when a sale is refunded, as it is used to measure the gross (not net) number of units sold. On the other hand, it does seem valid to reduce it on the following occasions:
Thinking of some of the scenarios in the original issue (these are places where
We fully appreciate different merchants may hold different opinions on this, but it does not seem a given that a product should not be regarded as best-selling, or unpopular, simply because it suffers a high number of refunds. I hope that makes sense, but let me know if you have any questions or concerns about this (that is, our stance on refunds, and how they should or should not impact |
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.
Besides the feedback provided in relation to handling of refunds, it feels like there would be value in adding unit tests for this functionality. Are you comfortable adding some? If not, we may be able to help/provide guidance.
| $order->update_status( 'processing' ); | ||
| $this->assertEquals( 1, $product->get_total_sales() ); | ||
|
|
||
| $order->update_status( 'cancelled' ); | ||
| $this->assertEquals( 0, $product->get_total_sales() ); | ||
|
|
||
| $order->update_status( 'processing' ); | ||
| $this->assertEquals( 1, $product->get_total_sales() ); | ||
|
|
||
| $order->update_status( 'completed' ); | ||
| $this->assertEquals( 1, $product->get_total_sales() ); | ||
|
|
||
| if ( $order->delete( true ) ) { | ||
| $this->assertEquals( 0, $product->get_total_sales() ); | ||
| } |
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.
This is great, but can we add one or more assertions relating to refunds to show our design choices as they relate to that scenario?
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.
@barryhughes I added 2 assertions to make sure that the transitions between refunded and processing statuses will not affect the sales count
|
Hi @Abdalsalaam! I was doing a further pass and realized we didn't address anything relating to order deletion. For consistency, it would seem to me like the following should generally be true:
However, there are exceptions:
With regards to permanent deletion, the following seems like a sensible policy:
What do you think? |
@barryhughes How can we add tests for (untrash) scenario? |
True! However, we can work at a higher level of abstraction rather than directly with the WP Posts API. The WC_Object_Data_Store_Interface includes a delete() method, and this is implemented by both the CPT and HPOS data-stores (and it supports trashing as well as permanent deletion). Does that help at all? |
|
@barryhughes yes that helps, i'll check what i can do! thank you |
|
Just a friendly note that code freeze for the 7.8.0 release happens on Monday next week (2023-05-22). This means that, realistically, if we want to include this fix in the 7.8.0 release then the PR will need to be updated and ready for a fresh review early this week (of course, it can also wait until some future point—I just wanted to give you an idea of the timelines we are working to re 7.8.0). |
|
@barryhughes thanks for letting me know, I need a help on writing the test using |
|
Hi @Abdalsalaam, There's a good example over here that does this by calling the order's delete method: $order = OrderHelper::create_order();
$order_id = $order->get_id();
$order->delete( true );Some levels below that, the data-store's delete method is invoked: $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) );In the test I linked to, we explicitly enable the HPOS data-store however, for this PR, i don't think that step is strictly necessary, as we already have a CI process that runs all unit tests against HPOS ("Run tests against PR in an environment with COT enabled"). |
|
Apologies, I misread your question. For untrashing, this test may be a better example. You should be able to get the underlying data-store from an order object as follows: $order->get_data_store(); |
|
@barryhughes I added a test for CPT |
| if ( $order->delete( false ) ) { | ||
| $this->assertEquals( 0, $product->get_total_sales() ); | ||
|
|
||
| wp_untrash_post( $order->get_id() ); |
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.
This won't work when running the test suite with HPOS enabled, but I see we have a gap in our implementation—bear with me while I think about addressing that.
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 the latest changes in trunk, it is now possible to do $order->untrash() (datastore-agnostic untrashing, instead of relying on WP functions) which should allow us to make this test work even when HPOS is enabled.
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.
@barryhughes Great! thank you for letting me know.
|
Hi , Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
barryhughes
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.
Great, thanks again! We need just a couple more tweaks to ensure our checks pass.
|
@barryhughes thanks a lot for your review! that was very helpful! |
Co-authored-by: Barry Hughes <[email protected]>
Co-authored-by: Barry Hughes <[email protected]>
|
With the most recent rebase (done to incorporate #39136), all checks are passing except for building the live branch (expected in this context). |
|
Testing instructions updated to cover some of the key evolutions in how this works. |
barryhughes
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.
Thanks for all the time you put into this, @Abdalsalaam. It's now good to merge.
|
@barryhughes thank you for your feedback and help on it |
|
For the record, the code in this pull request incorrectly uses the However by the time this pull request was merged All of this is fixed as part of #37050: it introduces the firing of |
Submission Review Guidelines:
Changes proposed in this Pull Request:
Closes #23796 .
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
total_salesproduct meta (either via the product editor, or directly using a database tool of your choosing), which may look something like: