Skip to content

fix(checkout): guard billing_zip_code v-if against undefined uses_postal_code#1318

Merged
superdav42 merged 3 commits into
mainfrom
fix/checkout-zip-uses-postal-code-guard
May 29, 2026
Merged

fix(checkout): guard billing_zip_code v-if against undefined uses_postal_code#1318
superdav42 merged 3 commits into
mainfrom
fix/checkout-zip-uses-postal-code-guard

Conversation

@superdav42

@superdav42 superdav42 commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

The billing_zip_code checkout field's Vue v-if expression references uses_postal_code without a guard. When older or minified checkout bundles render before the Vue data key has been registered, the expression throws a ReferenceError and the entire checkout form fails to render.

This change wraps the symbol with typeof X === 'undefined' so the form falls back to showing the ZIP field instead of crashing.

The bug

inc/checkout/signup-fields/class-signup-field-billing-address.php:355 (before):

$field['wrapper_html_attr']['v-if'] = "(order === false || order.should_collect_payment) && !($self_billing_gateways) && uses_postal_code";

If the Vue instance attempts to evaluate this expression before uses_postal_code has been registered as a data key, Vue throws ReferenceError: uses_postal_code is not defined and the surrounding <div v-if> collapses — taking the entire billing block with it on some renderings.

The fix

$field['wrapper_html_attr']['v-if'] = "(order === false || order.should_collect_payment) && !($self_billing_gateways) && (typeof uses_postal_code === 'undefined' || uses_postal_code)";

Defaulting to visible when the symbol is undefined is the safe choice:

  • The ZIP field still validates and submits correctly.
  • The symbol becomes defined on subsequent re-renders (data registration is eventual).
  • Countries that genuinely don't use postal codes (Hong Kong, UAE, Angola — see Country_Default) hide the field once uses_postal_code is defined and falsy, so the previous behaviour is preserved in steady state.

The alternative — defaulting to hidden when undefined — would prevent users from filling in the ZIP at all on the first render, which is a worse failure mode than showing a field that may not strictly be required.

Test

Adds test_zip_visibility_guards_uses_postal_code_symbol to tests/WP_Ultimo/Checkout/Signup_Fields/Signup_Field_Billing_Address_Test.php:

public function test_zip_visibility_guards_uses_postal_code_symbol(): void {
    $fields = $this->field->to_fields_array(
        array(
            'id'              => 'billing_address',
            'zip_and_country' => true,
            'element_classes' => '',
        )
    );

    $this->assertArrayHasKey( 'billing_zip_code', $fields );
    $this->assertArrayHasKey( 'v-if', $fields['billing_zip_code']['wrapper_html_attr'] );
    $this->assertStringContainsString( "typeof uses_postal_code === 'undefined'", $fields['billing_zip_code']['wrapper_html_attr']['v-if'] );
}

This asserts the guard is present in the generated wrapper attribute so future refactors don't quietly drop it.

Verification

  • vendor/bin/phpcs --standard=.phpcs.xml.dist <both files> → clean (0 errors, 0 warnings).
  • vendor/bin/phpstan analyse <both files> → clean (No errors).
  • vendor/bin/phpunit --filter test_zip_visibility_guards_uses_postal_code_symbol …OK (1 test, 3 assertions).

Scope

Pure defensive guard against a JS runtime error. No behaviour change in steady state. No new dependencies, no schema change, no migration.

Files touched:

  • inc/checkout/signup-fields/class-signup-field-billing-address.php — 1 line changed (the v-if string), 3 lines added (docblock explanation).
  • tests/WP_Ultimo/Checkout/Signup_Fields/Signup_Field_Billing_Address_Test.php — 1 new test method (19 lines including docblock and arrange/act/assert).

Total: +23 / −1.


aidevops.sh v3.20.5 plugin for OpenCode v1.15.12 with claude-opus-4-7 spent 1d 13h and 655,199 tokens on this with the user in an interactive session.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Enhanced stability of the billing zip code field in checkout by adding protective guards to the visibility logic, ensuring compatibility with older or minified checkout bundles and preventing potential rendering errors.
  • Tests

    • Added regression test to verify the billing zip code field visibility guard functions correctly across different bundle versions and configurations.

Review Change Stack

…tal_code

The billing_zip_code field's Vue v-if expression references uses_postal_code
without a guard. When older or minified checkout bundles render before the
Vue data key has been registered (timing-dependent on order of script
loading / hydration), the expression throws a ReferenceError and the
checkout form fails to render at all.

Wrap the symbol with typeof X === 'undefined' so the form falls back to
showing the ZIP field instead of crashing. Defaulting to visible is the
safe choice — the field still validates and submits, and the symbol
becomes defined on subsequent re-renders.

Includes a regression test asserting the guard is present in the
generated wrapper attribute so future refactors don't quietly drop it.
@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 24c1db9a-6a76-48ee-b660-29cc511e3159

📥 Commits

Reviewing files that changed from the base of the PR and between f1b5ab6 and 0e96172.

📒 Files selected for processing (2)
  • inc/checkout/signup-fields/class-signup-field-billing-address.php
  • tests/WP_Ultimo/Checkout/Signup_Fields/Signup_Field_Billing_Address_Test.php

📝 Walkthrough

Walkthrough

The PR adds a safety guard to prevent ReferenceError when the uses_postal_code Vue data key is unavailable in older checkout bundles. The billing_zip_code field's v-if condition now checks typeof uses_postal_code === 'undefined' before evaluating the symbol, with a corresponding regression test validating the guard's presence.

Changes

Billing Zip Code Visibility Guard

Layer / File(s) Summary
Vue v-if safety guard for postal code availability
inc/checkout/signup-fields/class-signup-field-billing-address.php, tests/WP_Ultimo/Checkout/Signup_Fields/Signup_Field_Billing_Address_Test.php
The billing_zip_code field's v-if expression is updated to wrap uses_postal_code with a typeof guard, and a new regression test verifies the guard is correctly generated in the wrapper's html attributes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

review-feedback-scanned, origin:worker

Poem

🐰 A guard on the postal key so bright,
Checks if it exists before the sight,
No more errors in bundles of old,
Vue renders safely, as planned is told! 📮

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and specifically describes the main change: adding a guard against undefined uses_postal_code in the billing_zip_code v-if condition.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/checkout-zip-uses-postal-code-guard

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
Copy Markdown

🔨 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
Copy Markdown

🔨 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
Copy Markdown

🔨 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 51d15ad into main May 29, 2026
8 of 11 checks passed
@superdav42 superdav42 deleted the fix/checkout-zip-uses-postal-code-guard branch May 29, 2026 21:47
@superdav42 superdav42 added the review-feedback-scanned Merged PR already scanned for quality feedback label May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-feedback-scanned Merged PR already scanned for quality feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant