Skip to content

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

Conversation

NeosinneR
Copy link
Member

@NeosinneR NeosinneR commented Aug 7, 2025

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:
Screenshot 2025-08-07 at 08 20 59

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:

Before After
Screenshot 2025-08-07 at 08 20 25 Screenshot 2025-08-07 at 08 20 45

How to test the changes in this Pull Request:

Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:

  1. Go to WooCommerce > Orders > Add orde.
  2. Add any product.
  3. Edit order item (the pencil icon).
  4. Update prices so Before discount is greater than Total.
  5. Click Save and check, that the discount is applied.

Testing that has already taken place:

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Aug 7, 2025
@NeosinneR NeosinneR force-pushed the wooplug-5271-wc-101-rc12-item-price-edit-is-broken-when-manually-editing branch from c837462 to cafaec6 Compare August 7, 2025 06:26
@NeosinneR NeosinneR added this to the 10.1.0 milestone Aug 7, 2025
This reverts 04db67b because now that the recalculation is not called, tax (additional $20) is also not shown
@NeosinneR NeosinneR marked this pull request as ready for review August 7, 2025 07:09
@github-actions github-actions bot added the focus: e2e tests Issues related to e2e tests label Aug 7, 2025
Copy link
Contributor

coderabbitai bot commented Aug 7, 2025

📝 Walkthrough

Walkthrough

This 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

Cohort / File(s) Change Summary
Changelog Update
plugins/woocommerce/changelog/wooplug-5271-wc-101-rc12-item-price-edit-is-broken-when-manually-editing
Added a changelog entry describing the patch that preserves manual discounts when editing order items manually.
Order Item Save Logic
plugins/woocommerce/includes/admin/wc-admin-functions.php
Modified wc_save_order_items to call $order->recalculate_coupons() only if the order has coupon items, preventing overwriting of manual discounts when no coupons are present.
E2E Test Update
plugins/woocommerce/tests/e2e-pw/tests/order/create-order.spec.js
Updated an assertion in the guest order creation test to expect "Paid: $200.00" instead of "Paid: $220.00". No other test logic changed.
Unit Test for Manual Discount Preservation
plugins/woocommerce/tests/legacy/unit-tests/discounts/discounts.php
Added a new unit test test_manual_discount_preservation_no_coupons to verify that manual discounts are preserved when saving order items with no coupons. Removed a comment from another test; no other functional 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
Loading

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 details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 657f323 and 6b5ab8c.

📒 Files selected for processing (1)
  • plugins/woocommerce/includes/admin/wc-admin-functions.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugins/woocommerce/includes/admin/wc-admin-functions.php
⏰ 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)
  • GitHub Check: Core e2e tests - WP latest-1 [WP 6.7.3] 3/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
  • GitHub Check: Core e2e tests - WP latest-1 [WP 6.7.3] 5/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - WP latest-1 [WP 6.7.3] 4/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - PHP 8.4 5/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - WP latest-1 [WP 6.7.3] 2/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - PHP 8.4 4/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - WP latest-1 [WP 6.7.3] 1/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - PHP 8.4 2/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - PHP 8.4 3/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - HPOS disabled - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - PHP 8.4 1/5 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg nightly 3/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg nightly 4/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg nightly 2/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg stable 4/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg stable 3/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg nightly 1/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg stable 2/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests - Gutenberg stable 1/4 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
  • GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.3] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
  • GitHub Check: Lint - @woocommerce/plugin-woocommerce
  • GitHub Check: build
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch wooplug-5271-wc-101-rc12-item-price-edit-is-broken-when-manually-editing

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

github-actions bot commented Aug 7, 2025

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

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.

@NeosinneR NeosinneR requested review from jorgeatorres, kalessil and a team August 7, 2025 07:24
Copy link
Contributor

github-actions bot commented Aug 7, 2025

Testing Guidelines

Hi @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:

  • 🖼️ Screenshots or screen recordings.
  • 📝 List of functionality tested / steps followed.
  • 🌐 Site details (environment attributes such as hosting type, plugins, theme, store size, store age, and relevant settings).
  • 🔍 Any analysis performed, such as assessing potential impacts on environment attributes and other plugins, conducting performance profiling, or using LLM/AI-based analysis.

⚠️ Within the testing details you provide, please ensure that no sensitive information (such as API keys, passwords, user data, etc.) is included in this public issue.

Copy link
Contributor

@yuliyan yuliyan left a 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' );
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 6b5ab8c

@NeosinneR
Copy link
Member Author

However, when a coupon is applied, modifying the item discount manually has no effect. Just wanted to confirm if this is the intended behavior.

@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.

@yuliyan yuliyan self-requested a review August 7, 2025 09:27
Copy link
Contributor

@yuliyan yuliyan left a comment

Choose a reason for hiding this comment

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

LGTM!

@yuliyan yuliyan assigned NeosinneR and unassigned yuliyan Aug 7, 2025
@NeosinneR NeosinneR merged commit 3de58cd into trunk Aug 7, 2025
65 checks passed
@NeosinneR NeosinneR deleted the wooplug-5271-wc-101-rc12-item-price-edit-is-broken-when-manually-editing branch August 7, 2025 09:40
github-actions bot pushed a commit that referenced this pull request Aug 7, 2025
* 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
Copy link
Contributor

github-actions bot commented Aug 7, 2025

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 release/10.1. Please merge the following PR: [Backport to release/10.1] Preserve manual discount when manually editing an order

NeosinneR added a commit that referenced this pull request Aug 7, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: e2e tests Issues related to e2e tests plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[WC 10.1 RC1/2] Item price edit is broken when manually editing an order
2 participants