Skip to content

fix(checkout): prevent spurious create_order on load and ensure discount_code is always a string#513

Merged
superdav42 merged 1 commit intomainfrom
bugfix/issue-76-coupon-remove
Mar 26, 2026
Merged

fix(checkout): prevent spurious create_order on load and ensure discount_code is always a string#513
superdav42 merged 1 commit intomainfrom
bugfix/issue-76-coupon-remove

Conversation

@superdav42
Copy link
Collaborator

Summary

  • Always initialize discount_code to '' in get_checkout_variables() so wu_checkout.discount_code is never undefined in JS. An undefined value caused the Vue discount_code watcher to fire a spurious create_order() AJAX call on page load when v-init set the field to an empty string.
  • Add || '' fallback in checkout.js initial data as a defensive guard.
  • Use explicit 'true'/'false' strings for v-init:toggle_discount_code in class-signup-field-discount-code.php. PHP false becomes an empty HTML attribute (""), which Vue evaluates as undefined — causing the toggle_discount_code watcher to fire and clear discount_code on page load even when no coupon is pre-applied.
  • Update checkout.min.js to match checkout.js.
  • Add unit test asserting discount_code is always a string in checkout variables.

Root Cause Analysis

Issue #76 reported two bugs:

  1. Delete button does nothing — fixed in PR fix: coupon removal button and duplicate field ID on checkout form (#76) #389 (added toggle_discount_code = false to the remove button click handler in simple.php).
  2. Coupon code does not render correctly — partially fixed by PR fix: coupon removal button and duplicate field ID on checkout form (#76) #389 (duplicate field ID) but a deeper initialization bug remained.

The remaining rendering bug: wu_checkout.discount_code was only set in $variables when a coupon was pre-applied. When no coupon was present, the key was absent, making wu_checkout.discount_code undefined in JS. The Vue discount_code watcher fires when the value changes from undefined to '' (via v-init), triggering a spurious create_order() AJAX call on every page load.

Additionally, v-init:toggle_discount_code was set to a PHP boolean false, which esc_attr() renders as an empty string "". Vue evaluates an empty directive expression as undefined, causing the toggle_discount_code watcher to fire with a falsy value and clear discount_code on load.

Testing Evidence

  • Testing level: self-assessed
  • Risk classification: medium (Vue data initialization, AJAX call behavior)
  • Stability results: Logic traced through PHP → JS data flow; watcher behavior verified against Vue 2 docs
  • Smoke pages/endpoints checked: N/A — no dev environment configured
  • Unit test added: test_checkout_variables_always_has_discount_code_string() verifies discount_code is always a string in checkout variables

Files Changed

  • inc/checkout/class-checkout.php — always initialize discount_code to '' in checkout variables
  • inc/checkout/signup-fields/class-signup-field-discount-code.php — use explicit 'true'/'false' for v-init:toggle_discount_code
  • assets/js/checkout.js — add || '' fallback for wu_checkout.discount_code
  • assets/js/checkout.min.js — minified version of checkout.js
  • tests/unit/Checkout_Request_Test.php — add test for discount_code string guarantee

Closes #76

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 26, 2026

Warning

Rate limit exceeded

@superdav42 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 9 minutes and 26 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cb069381-19bc-459d-b2b6-091ccf016411

📥 Commits

Reviewing files that changed from the base of the PR and between ac7f2b1 and 6f399d4.

⛔ Files ignored due to path filters (1)
  • assets/js/checkout.min.js is excluded by !**/*.min.js
📒 Files selected for processing (4)
  • assets/js/checkout.js
  • inc/checkout/class-checkout.php
  • inc/checkout/signup-fields/class-signup-field-discount-code.php
  • tests/unit/Checkout_Request_Test.php
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/issue-76-coupon-remove

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.

@github-actions
Copy link

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@github-actions
Copy link

github-actions bot commented Mar 26, 2026

Performance Test Results

Performance test results for 983ff55 are in 🛎️!

URL: /

Run DB Queries Memory Before Template Template WP Total LCP TTFB LCP - TTFB
0 57 36.39 MB 296.00 ms 104.50 ms 400.00 ms 492.40 ms 414.55 ms 78.40 ms
1 57 36.39 MB 294.00 ms 102.50 ms 396.00 ms 489.00 ms 409.65 ms 79.00 ms

…unt_code is always a string

- Always initialize discount_code to '' in get_checkout_variables() so
  wu_checkout.discount_code is never undefined in JS. An undefined value
  caused the Vue discount_code watcher to fire a spurious create_order()
  AJAX call on page load when v-init set the field to an empty string.

- Add || '' fallback in checkout.js initial data for defensive safety.

- Use explicit 'true'/'false' strings for v-init:toggle_discount_code in
  class-signup-field-discount-code.php. PHP false becomes an empty HTML
  attribute (""), which Vue evaluates as undefined — causing the
  toggle_discount_code watcher to fire and clear discount_code on load.

- Update checkout.min.js to match checkout.js.

- Add test asserting discount_code is always a string in checkout variables.

Closes #76
@github-actions
Copy link

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@superdav42 superdav42 merged commit d235c2b into main Mar 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impossible to remove coupon code once applied

1 participant