fix(checkout): relax optional postal code validation#1400
Conversation
📝 WalkthroughWalkthroughThis PR relaxes validation rules for optional billing-address fields during multi-step checkout submission. The implementation computes normalized submitted billing country, conditionally adjusts validation rules for billing-country and billing-zip-code fields when not explicitly required, and updates country normalization to trim and fallback properly. A new test verifies the behavior. ChangesBilling Address Validation Relaxation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
🔨 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! Login credentials: |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@inc/checkout/class-checkout.php`:
- Around line 2773-2783: The loop that relaxes billing rules only inspects
$this->step['fields'], so optional_billing_rules are not applied for billing
fields defined on earlier steps and final validation can re-enforce
required_with:billing_zip_code; change the relaxation logic to iterate all
checkout steps' fields (not just the current $this->step['fields']) so that
whenever a field_id exists in $optional_billing_rules you set
$validation_rules[$field_id] = $optional_billing_rules[$field_id]; update the
code that builds validation_rules (where $optional_billing_rules and
$validation_rules are referenced) to scan the global steps/fields collection
stored on the checkout form/session and ensure session-backed values are
considered, and add a regression test that places billing_country and
billing_zip_code on step 1 and submits on step 2 to confirm the override
persists.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ba20fced-3fa2-44ce-ae85-e4638573e0ba
📒 Files selected for processing (2)
inc/checkout/class-checkout.phptests/WP_Ultimo/Checkout/Checkout_Test.php
| foreach ($this->step['fields'] as $field_key => $field) { | ||
| if ( ! is_array($field)) { | ||
| continue; | ||
| } | ||
|
|
||
| $field_id = wu_get_isset($field, 'id', is_string($field_key) ? $field_key : ''); | ||
|
|
||
| if (isset($optional_billing_rules[ $field_id ]) && ! wu_get_isset($field, 'required')) { | ||
| $validation_rules[ $field_id ] = $optional_billing_rules[ $field_id ]; | ||
| } | ||
| } |
There was a problem hiding this comment.
Optional billing override only checks the current step, so final validation can re-fail blank ZIP values.
At Line 2773, the relaxation loop scans only $this->step['fields']. In multi-step checkout, final validation still evaluates full rules with session-backed values; if billing fields were on an earlier step, the override is skipped and base required_with:billing_zip_code can fail again.
Suggested fix
- foreach ($this->step['fields'] as $field_key => $field) {
+ $billing_rule_fields = $this->checkout_form
+ ? $this->checkout_form->get_all_fields()
+ : wu_get_isset($this->step, 'fields', []);
+
+ foreach ($billing_rule_fields as $field_key => $field) {
if ( ! is_array($field)) {
continue;
}Also add a regression test where billing_country/billing_zip_code are on step 1 and submission happens on step 2.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@inc/checkout/class-checkout.php` around lines 2773 - 2783, The loop that
relaxes billing rules only inspects $this->step['fields'], so
optional_billing_rules are not applied for billing fields defined on earlier
steps and final validation can re-enforce required_with:billing_zip_code; change
the relaxation logic to iterate all checkout steps' fields (not just the current
$this->step['fields']) so that whenever a field_id exists in
$optional_billing_rules you set $validation_rules[$field_id] =
$optional_billing_rules[$field_id]; update the code that builds validation_rules
(where $optional_billing_rules and $validation_rules are referenced) to scan the
global steps/fields collection stored on the checkout form/session and ensure
session-backed values are considered, and add a regression test that places
billing_country and billing_zip_code on step 1 and submits on step 2 to confirm
the override persists.
Summary
Verification
Merged via PR #1400 to main. aidevops.sh v3.20.52 spent 15m on this as a headless bash routine. |
Summary
required_with:billing_zip_codeserver rule.Verification
vendor/bin/phpcs inc/checkout/class-checkout.phpvendor/bin/phpunit --filter 'test_get_validation_rules_relaxes_billing_for_free|test_get_validation_rules_relaxes_optional_billing_address_fields|Signup_Field_Billing_Address_Test'git diff --checkSummary by CodeRabbit
Release Notes
Bug Fixes
Tests