Skip to content

Revert recalculating coupons on order save - #61091

Merged
NeosinneR merged 3 commits into
trunkfrom
wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
Sep 24, 2025
Merged

NeosinneR merged 3 commits into
trunkfrom
wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals

Conversation

@NeosinneR

@NeosinneR NeosinneR commented Sep 23, 2025 •

Copy link
Copy Markdown
Member

Submission Review Guidelines:

Changes proposed in this Pull Request:

This PR reverts #59606 and #60240.

The original issue was, that on an order with a coupon discount, when the quantity is changed, the discount would also change, surpassing the coupon's value. The fix was to recalculate coupons when order item is saved.

But this came with a couple of unintended side effects:

  1. Recalculating on order stats change, without any changes in items. This sometimes caused changes in taxes and totals, which no longer match what the customer paid for.
  2. Any custom hooks into coupons value was not respected when updating order.
  3. When coupon expired and order was updated, the discount would disappear.

So instead of trying to fix these issue, I chose to revert the original fix, as it caused more harm than good.

Closes #60661, closes WOOPLUG-5447, closes #61080, closes WOOPLUG-5578, closes #61025, closes WOOPLUG-5563, closes #60553, closes WOOPLUG-5400, closes #61409, closes WOOPLUG-5690.

How to test the changes in this Pull Request:

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

Follow steps in #61025, #60661, and #61080 that they are no longer reproducible.

Testing that has already taken place:

I've followed steps to reproduce from the above mentioned three issues to verify, they no longer were possible.

@NeosinneR NeosinneR changed the title Reverts recalculating coupons on order save Revert recalculating coupons on order save Sep 23, 2025
@github-actions github-actions Bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Sep 23, 2025
This test was testing that after saving the order, order item discount and total was recalculated. But since we removed that functionality, this test no longer server any purpose
@github-actions

github-actions Bot commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Testing Guidelines

Hi @yuliyan @tjcafferkey ,

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.

@NeosinneR
NeosinneR marked this pull request as ready for review September 23, 2025 20:17
@coderabbitai

coderabbitai Bot commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Removes automatic coupon recalculation from wc_save_order_items, updates a legacy unit test by deleting a coupon-related scenario, and adds a changelog entry documenting the behavior change.

Changes

Cohort / File(s) Summary
Admin order save logic
plugins/woocommerce/includes/admin/wc-admin-functions.php
Deleted the block that re-applied coupons in wc_save_order_items; order totals now recalc without recalculate_coupons().
Legacy tests
plugins/woocommerce/tests/legacy/unit-tests/discounts/discounts.php
Removed test method test_fixed_cart_discount_quantity_change_admin_order related to coupon recalculation on admin item quantity change.
Changelog
plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
Added changelog entry noting no coupon recalculation on order save and avoiding totals/taxes changes when unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Admin as Admin (WP Admin)
  participant UI as Order Edit UI
  participant WC as wc_save_order_items()
  participant Order as WC_Order

  Admin->>UI: Update order items
  UI->>WC: Save items (POST)
  WC->>Order: Save line items
  Note over WC,Order: Coupon re-application step removed
  WC->>Order: calculate_totals(false)
  Order-->>WC: Totals updated (no coupon recalculation)
  WC-->>UI: Response (success)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title Check ✅ Passed The title "Revert recalculating coupons on order save" is concise, specific, and accurately describes the primary change in the diff (removing coupon recalculation on order save). It highlights the main intent from the developer's perspective and is easy for teammates scanning history to understand. The title avoids noise and correctly reflects the revert action taken in the PR.
Linked Issues Check ✅ Passed The change removes the call to recalculate_coupons() in wc_save_order_items and removes a related unit test, which directly addresses preserving finalized totals and shipping/fee taxes [#60661], preventing coupon re-evaluation on status changes [#61080], and allowing manual shipping tax edits to persist when coupons are present [#61025]. The raw_summary shows only those targeted changes and the PR description documents testing against the referenced issues. Based on the provided diffs and objectives, the code modifications meet the coding-related requirements, subject to verifying that calculate_totals(false) or other save-side hooks do not re-trigger the undesired recalculations.
Out of Scope Changes Check ✅ Passed All modified files are a changelog entry, the admin wc_save_order_items change (removing coupon recalculation), and a related legacy unit test removal; these edits are directly aligned with the linked issues and there are no unrelated code changes present in the provided summary. Nothing in the diff suggests out-of-scope modifications.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed The PR description directly describes reverting the coupon-recalculation change, cites the specific commits being reverted, explains the rationale and observed side effects, references the related issues it closes, and provides testing steps; these details align with the diffs that remove coupon recalculation in wc_save_order_items and the associated test, so the description is relevant to the changeset.
✨ 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-5447-save-order-adds-vat-to-shippingfees-and-changes-totals

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2a6002 and bdac79b.

📒 Files selected for processing (3)
  • plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals (1 hunks)
  • plugins/woocommerce/includes/admin/wc-admin-functions.php (0 hunks)
  • plugins/woocommerce/tests/legacy/unit-tests/discounts/discounts.php (0 hunks)
💤 Files with no reviewable changes (2)
  • plugins/woocommerce/tests/legacy/unit-tests/discounts/discounts.php
  • plugins/woocommerce/includes/admin/wc-admin-functions.php
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: mikejolley
PR: woocommerce/woocommerce#60980
File: plugins/woocommerce/src/RestApi/Routes/V4/Orders/UpdateUtils.php:111-118
Timestamp: 2025-09-22T15:56:45.594Z
Learning: The WC_Order::recalculate_coupons() method internally handles all necessary total recalculations when coupons are updated, making additional calculate_totals() calls redundant in the coupon update flow.
Learnt from: mikejolley
PR: woocommerce/woocommerce#60980
File: plugins/woocommerce/src/RestApi/Routes/V4/Orders/UpdateUtils.php:111-118
Timestamp: 2025-09-22T15:56:45.593Z
Learning: The WC_Order::recalculate_coupons() method internally calls calculate_totals(true), so when coupon_lines are updated, order totals are automatically recalculated without needing an additional calculate_totals() call.
📚 Learning: 2025-09-22T15:56:45.594Z
Learnt from: mikejolley
PR: woocommerce/woocommerce#60980
File: plugins/woocommerce/src/RestApi/Routes/V4/Orders/UpdateUtils.php:111-118
Timestamp: 2025-09-22T15:56:45.594Z
Learning: The WC_Order::recalculate_coupons() method internally handles all necessary total recalculations when coupons are updated, making additional calculate_totals() calls redundant in the coupon update flow.

Applied to files:

  • plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
📚 Learning: 2025-09-22T15:56:45.593Z
Learnt from: mikejolley
PR: woocommerce/woocommerce#60980
File: plugins/woocommerce/src/RestApi/Routes/V4/Orders/UpdateUtils.php:111-118
Timestamp: 2025-09-22T15:56:45.593Z
Learning: The WC_Order::recalculate_coupons() method internally calls calculate_totals(true), so when coupon_lines are updated, order totals are automatically recalculated without needing an additional calculate_totals() call.

Applied to files:

  • plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
📚 Learning: 2025-09-22T15:56:45.594Z
Learnt from: mikejolley
PR: woocommerce/woocommerce#60980
File: plugins/woocommerce/src/RestApi/Routes/V4/Orders/UpdateUtils.php:111-118
Timestamp: 2025-09-22T15:56:45.594Z
Learning: The WC_Order::recalculate_coupons() method internally calls $this->calculate_totals(true) at the end of the method, so when coupon_lines are updated in REST API requests, order totals are automatically recalculated without needing an additional calculate_totals() call.

Applied to files:

  • plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
⏰ 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). (1)
  • GitHub Check: build
🔇 Additional comments (2)
plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals (2)

1-1: Significance looks right.

"patch" matches the scope of reverting a behavior and fixing regressions.


2-2: Change Type: update → fix

This entry is a bug/regression; use Type: fix for release tooling and reader expectations.

-Significance: patch
-Type: update
+Significance: patch
+Type: fix

Significance: patch
Type: update

Don't recalculate coupons on order save, which can cause recalculation of totals and taxes when nothing changed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Prefix with Comment: and use imperative style (avoid contraction).

Changelog parser expects a Comment: line; without it, the entry may be ignored or rendered poorly.

Apply this diff:

-Don't recalculate coupons on order save, which can cause recalculation of totals and taxes when nothing changed
+Comment: Do not recalculate coupons on order save to avoid changing totals and taxes when nothing changed.

Optionally include references (helps triage and release notes):

+Issue: #60661, #61080, #61025
+Refs: #59606, #60240
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Don't recalculate coupons on order save, which can cause recalculation of totals and taxes when nothing changed
Comment: Do not recalculate coupons on order save to avoid changing totals and taxes when nothing changed.
Issue: #60661, #61080, #61025
Refs: #59606, #60240
🤖 Prompt for AI Agents
In
plugins/woocommerce/changelog/wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals
around line 4, the changelog entry is missing the required "Comment:" prefix and
uses non‑imperative/contracted wording; update the line to start with "Comment:"
and rewrite the sentence in imperative, non‑contracted form (for example:
"Comment: Do not recalculate coupons on order save, which can cause
recalculation of totals and taxes when nothing has changed"). Optionally append
references or issue IDs after the Comment: line for triage and release notes.

@github-actions

Copy link
Copy Markdown
Contributor

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.

@yuliyan yuliyan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confirming the revert fixes the problems in the linked issue.

@NeosinneR
NeosinneR merged commit 916a6cc into trunk Sep 24, 2025
@NeosinneR
NeosinneR deleted the wooplug-5447-save-order-adds-vat-to-shippingfees-and-changes-totals branch September 24, 2025 08:52
@github-actions github-actions Bot added this to the 10.3.0 milestone Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment