diff --git a/inc/checkout/class-checkout.php b/inc/checkout/class-checkout.php index d1071782..da863c37 100644 --- a/inc/checkout/class-checkout.php +++ b/inc/checkout/class-checkout.php @@ -2770,7 +2770,9 @@ public function get_validation_rules() { 'billing_zip_code' => '', ]; - foreach ($this->step['fields'] as $field_key => $field) { + $billing_rule_fields = $this->checkout_form ? $this->checkout_form->get_all_fields() : $this->step['fields']; + + foreach ($billing_rule_fields as $field_key => $field) { if ( ! is_array($field)) { continue; } diff --git a/tests/WP_Ultimo/Checkout/Checkout_Test.php b/tests/WP_Ultimo/Checkout/Checkout_Test.php index e03ba0a8..b8801263 100644 --- a/tests/WP_Ultimo/Checkout/Checkout_Test.php +++ b/tests/WP_Ultimo/Checkout/Checkout_Test.php @@ -1622,6 +1622,67 @@ public function test_get_validation_rules_relaxes_optional_billing_address_field unset($_REQUEST['billing_country'], $_REQUEST['billing_zip_code'], $_REQUEST['user_id']); } + /** + * Test optional billing fields on earlier checkout steps relax final validation. + */ + public function test_get_validation_rules_relaxes_optional_billing_address_fields_from_all_steps(): void { + + $form = new \WP_Ultimo\Models\Checkout_Form([ + 'name' => 'Optional Billing Multi-step ' . time(), + 'slug' => 'optional-billing-multi-step-' . time(), + 'settings' => [ + [ + 'id' => 'billing-step', + 'name' => 'Billing Step', + 'fields' => [ + [ + 'id' => 'billing_country', + 'type' => 'select', + ], + [ + 'id' => 'billing_zip_code', + 'type' => 'text', + ], + ], + ], + [ + 'id' => 'final-step', + 'name' => 'Final Step', + 'fields' => [ + [ + 'id' => 'site_title', + 'type' => 'text', + ], + ], + ], + ], + ]); + + $checkout = Checkout::get_instance(); + $checkout->checkout_form = $form; + $checkout->step = $form->get_step('final-step', true); + $checkout->steps = $form->get_steps_to_show(); + $checkout->step_name = 'final-step'; + + $this->ensure_session($checkout); + + unset($_REQUEST['pre-flight'], $_REQUEST['checkout_form']); + + $_REQUEST['billing_country'] = 'US'; + $_REQUEST['billing_zip_code'] = ''; + $_REQUEST['user_id'] = self::$customer->get_user_id(); + + $rules = $checkout->get_validation_rules(); + + $this->assertSame('country', $rules['billing_country']); + $this->assertSame('', $rules['billing_zip_code']); + $this->assertTrue($checkout->validate($rules)); + + unset($_REQUEST['billing_country'], $_REQUEST['billing_zip_code'], $_REQUEST['user_id']); + + $checkout->checkout_form = null; + } + /** * Test get_validation_rules returns array. */