-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Preserve manual discount when manually editing an order #60240
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
Preserve manual discount when manually editing an order #60240
Conversation
… discount is used
c837462
to
cafaec6
Compare
This reverts 04db67b because now that the recalculation is not called, tax (additional $20) is also not shown
📝 WalkthroughWalkthroughThis change modifies WooCommerce order item saving logic to conditionally recalculate coupons only when coupons are present, ensuring manual discounts are preserved during manual order edits. It includes a new unit test for manual discount preservation, updates an end-to-end test expectation, and adds a changelog entry documenting the fix. Changes
Sequence Diagram(s)sequenceDiagram
participant Admin as Admin User
participant WC as WooCommerce (Order Admin Functions)
participant Order as Order Object
Admin->>WC: Edit order items (manual discount)
WC->>Order: Update order items and taxes
WC->>Order: Check for coupon items
alt Coupons present
WC->>Order: Recalculate coupons
else No coupons
Note right of WC: Skip coupon recalculation
end
WC->>Order: Save order
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: .coderabbit.yml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (32)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
Testing GuidelinesHi @yuliyan , Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed. Reminder: PR reviewers are required to document testing performed. This includes:
|
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.
I've confirmed that with these changes, adding a manual discount works as expected when no coupon is applied.
However, when a coupon is applied, modifying the item discount manually has no effect. Just wanted to confirm if this is the intended behavior.
|
||
// Only recalculate when a coupon is applied. | ||
// This allows manual discounts to be preserved when order items are saved. | ||
$order_coupons = $order->get_items( 'coupon' ); |
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.
Nit: Could use $order->get_coupons()
instead.
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.
Fixed in 6b5ab8c
@yuliyan correct. I also noticed this. I don't think there is anything we can do, because the coupon code is not directly tied to specific order item, but rather it's proportionally divided to all order items. And to preserve the original fix from #59606 to prevent applying discount bigger than the coupon amount, any other manual discount is discarded. |
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.
LGTM!
* Only recalculate coupons when real coupon is applied, not when manual discount is used * Add changelog * Add test * Update test This reverts 04db67b because now that the recalculation is not called, tax (additional $20) is also not shown * Better method to fetch coupons
IMPORTANT: Merging this PR to the appropriate branches is critical to the release process and ensures that the bug does not cause regressions in the future releases. Cherry picking was successful for |
…ting an order (#60248) Preserve manual discount when manually editing an order (#60240) * Only recalculate coupons when real coupon is applied, not when manual discount is used * Add changelog * Add test * Update test This reverts 04db67b because now that the recalculation is not called, tax (additional $20) is also not shown * Better method to fetch coupons Co-authored-by: Ján Mikláš <neosinner@gmail.com>
Submission Review Guidelines:
Changes proposed in this Pull Request:
Note: this is targeting 10.1 milestone, to be included in next week's 10.1 release.
In #59606 we added a fix, that when manually editing an order with a coupon code, increasing item quantity wouldn't cause the discount to be greater than the coupon's value. This was done by calling
$order->recalculate_coupons();
on order items save.However, this introduced an issue when manually adding a discount to the order without using any coupon code:

In this case, calling
$order->recalculate_coupons();
removes the discount, because there is no coupon associated so the order items totals are recalculated to remove the discount (i.e., so the discount is not greater than $0).The fix is to only call
$order->recalculate_coupons();
when a coupon code is associated with the order.Closes #60235, closes WOOPLUG-5271.
(For Bug Fixes) Bug introduced in PR #59606.
Screenshots or screen recordings:
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
Before discount
is greater thanTotal
.Save
and check, that the discount is applied.Testing that has already taken place: