Skip to content

fix(checkout): relax optional postal code validation#1400

Merged
superdav42 merged 1 commit into
mainfrom
fix/checkout-optional-postal-code-validation
Jun 10, 2026
Merged

fix(checkout): relax optional postal code validation#1400
superdav42 merged 1 commit into
mainfrom
fix/checkout-optional-postal-code-validation

Conversation

@superdav42

@superdav42 superdav42 commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Relax optional billing-address validation so an empty postal code no longer trips the self-referential required_with:billing_zip_code server rule.
  • Preserve country validation when an optional country is supplied, while allowing the optional country field to remain blank.
  • Add a regression test covering optional billing country + blank postal code validation.

Verification

  • vendor/bin/phpcs inc/checkout/class-checkout.php
  • vendor/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 --check

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved validation handling for optional billing address fields during multi-step checkout to reduce unnecessary validation errors.
    • Enhanced billing country detection logic for more reliable form submission processing.
  • Tests

    • Added test coverage for optional billing address field validation behavior.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

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

Changes

Billing Address Validation Relaxation

Layer / File(s) Summary
Optional billing field validation relaxation
inc/checkout/class-checkout.php
Relaxes validation rules for optional billing-address fields by computing normalized submitted billing_country, defining conditional rules that preserve billing_country validation only when non-empty and clear billing_zip_code validation, then applying overrides per step field when not explicitly required. Country field normalization is updated to trim billing_country and fallback to country when empty.
Test coverage for optional billing rules relaxation
tests/WP_Ultimo/Checkout/Checkout_Test.php
New test verifies optional billing ZIP rules are cleared when submitted as empty while ensuring billing_country retains expected rule mapping. Test prepares checkout and session state, sets request inputs, asserts generated validation rules, validates with those rules, and restores request state.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

review-feedback-scanned

Poem

🐰 Bills need not all fields filled,
When steps are taken one by one—
Zip codes relax, countries stay skilled,
Optional validation's fun!
Thump thump!

🚥 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 clearly and specifically describes the main change: relaxing validation for optional postal code fields in the checkout process, which aligns with the core modifications to billing-address validation logic.
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-optional-postal-code-validation

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

@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

🤖 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

📥 Commits

Reviewing files that changed from the base of the PR and between be2a59c and 1360aab.

📒 Files selected for processing (2)
  • inc/checkout/class-checkout.php
  • tests/WP_Ultimo/Checkout/Checkout_Test.php

Comment on lines +2773 to +2783
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 ];
}
}

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 | 🟠 Major | ⚡ Quick win

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.

@superdav42 superdav42 merged commit b286760 into main Jun 10, 2026
11 checks passed
@superdav42

Copy link
Copy Markdown
Collaborator Author

Summary

  • Relax optional billing-address validation so an empty postal code no longer trips the self-referential required_with:billing_zip_code server rule.
  • Preserve country validation when an optional country is supplied, while allowing the optional country field to remain blank.
  • Add a regression test covering optional billing country + blank postal code validation.

Verification

  • vendor/bin/phpcs inc/checkout/class-checkout.php
  • vendor/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 --check

Merged via PR #1400 to main.
Merged by deterministic merge pass (pulse-wrapper.sh).


aidevops.sh v3.20.52 spent 15m on this as a headless bash routine.

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