Commit 0e0e6eb19a7 for woocommerce
commit 0e0e6eb19a7b853a01976b67b3a1c206c7775f7e
Author: Mike Jolley <mike.jolley@me.com>
Date: Wed Nov 25 16:03:51 2026 +0000
Build the classic checkout billing phone from the default address fields (#70929)
* Build the billing phone from the default address fields
Since 10.8.0 phone is a default address field, and address-i18n.js applies
its locale entry on the classic checkout. get_address_fields() still
replaced billing_phone with a hard-coded copy after the locale step, so
woocommerce_default_address_fields and country locale rules reached the
script but not the server. A field could look optional, or be hidden,
while the server still required it.
Billing phone now comes from the default fields, like shipping phone and
the other address fields. The docs now list phone as a default address
field and use woocommerce_default_address_fields to make it optional.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Add changelog entry for the billing phone fix
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/docs/code-snippets/customising-checkout-fields.md b/docs/code-snippets/customising-checkout-fields.md
index cee2a90fbb5..45340f5aeb1 100644
--- a/docs/code-snippets/customising-checkout-fields.md
+++ b/docs/code-snippets/customising-checkout-fields.md
@@ -147,6 +147,7 @@ Here's a full list of fields in the array passed to `woocommerce_checkout_fields
- `shipping_postcode`
- `shipping_country`
- `shipping_state`
+ - `shipping_phone`
- Account
- `account_username`
- `account_password`
@@ -176,6 +177,9 @@ In specific cases you need to use the **`woocommerce_default_address_fields`** f
- `city`
- `state`
- `postcode`
+- `phone`
+
+On the shortcode checkout, the address, city, state, postcode and phone fields also follow country-specific rules. The checkout applies these rules again when the customer changes their country. So if you change the label, required state or priority of these fields with `woocommerce_checkout_fields` or `woocommerce_billing_fields`, the change can be lost. Use `woocommerce_default_address_fields` for these fields instead.
For example, to make the `address_1` field optional:
@@ -399,11 +403,15 @@ This is the result:
### Make phone number not required
+Phone is a default address field, so this changes both the billing and the shipping phone:
+
```php
-add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
+add_filter( 'woocommerce_default_address_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
- $address_fields['billing_phone']['required'] = false;
+ if ( isset( $address_fields['phone'] ) ) {
+ $address_fields['phone']['required'] = false;
+ }
return $address_fields;
}
```
diff --git a/plugins/woocommerce/changelog/69355-billing-phone-follows-address-fields b/plugins/woocommerce/changelog/69355-billing-phone-follows-address-fields
new file mode 100644
index 00000000000..c63ca8b249b
--- /dev/null
+++ b/plugins/woocommerce/changelog/69355-billing-phone-follows-address-fields
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Stop the classic checkout from requiring a billing phone that address field customisations make optional or hide.
diff --git a/plugins/woocommerce/includes/class-wc-countries.php b/plugins/woocommerce/includes/class-wc-countries.php
index 11e1cbf042c..0fb8bbd1fdf 100644
--- a/plugins/woocommerce/includes/class-wc-countries.php
+++ b/plugins/woocommerce/includes/class-wc-countries.php
@@ -2020,19 +2020,8 @@ class WC_Countries {
$address_fields[ $type . $key ] = $value;
}
- // Add email and phone fields.
+ // Add the email field.
if ( 'billing_' === $type ) {
- if ( 'hidden' !== CartCheckoutUtils::get_phone_field_visibility() ) {
- $address_fields['billing_phone'] = array(
- 'label' => __( 'Phone', 'woocommerce' ),
- 'required' => 'required' === CartCheckoutUtils::get_phone_field_visibility(),
- 'type' => 'tel',
- 'class' => array( 'form-row-wide' ),
- 'validate' => array( 'phone' ),
- 'autocomplete' => 'section-' . $address_type . ' ' . $address_type . ' tel',
- 'priority' => 100,
- );
- }
$address_fields['billing_email'] = array(
'label' => __( 'Email address', 'woocommerce' ),
'required' => true,
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-countries-test.php b/plugins/woocommerce/tests/php/includes/class-wc-countries-test.php
index 090981b8080..e3f1b745fa5 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-countries-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-countries-test.php
@@ -999,6 +999,86 @@ class WC_Countries_Test extends \WC_Unit_Test_Case {
$this->assertTrue( $fields['billing_state']['required'], 'Benin keeps its required list of 12 departments.' );
}
+ /**
+ * @testdox 'get_address_fields' builds the billing phone from the phone setting, like the other address fields.
+ *
+ * @testWith ["required", true]
+ * ["optional", false]
+ *
+ * @param string $visibility Phone field setting.
+ * @param bool $required Expected required state.
+ */
+ public function test_get_address_fields_builds_billing_phone_from_the_phone_setting( string $visibility, bool $required ): void {
+ update_option( 'woocommerce_checkout_phone_field', $visibility );
+
+ $fields = ( new WC_Countries() )->get_address_fields( 'US', 'billing_' );
+
+ $this->assertSame(
+ array(
+ 'label' => 'Phone',
+ 'required' => $required,
+ 'type' => 'tel',
+ 'class' => array( 'form-row-wide' ),
+ 'validate' => array( 'phone' ),
+ 'autocomplete' => 'section-billing billing tel',
+ 'priority' => 100,
+ ),
+ $fields['billing_phone']
+ );
+ }
+
+ /**
+ * @testdox 'get_address_fields' leaves out the billing phone when the phone setting is hidden.
+ */
+ public function test_get_address_fields_leaves_out_billing_phone_when_the_phone_setting_is_hidden(): void {
+ update_option( 'woocommerce_checkout_phone_field', 'hidden' );
+
+ $fields = ( new WC_Countries() )->get_address_fields( 'US', 'billing_' );
+
+ $this->assertArrayNotHasKey( 'billing_phone', $fields );
+ }
+
+ /**
+ * @testdox 'get_address_fields' applies 'woocommerce_default_address_fields' to the billing phone, as the address-i18n script does.
+ */
+ public function test_get_address_fields_applies_default_address_fields_filter_to_billing_phone(): void {
+ update_option( 'woocommerce_checkout_phone_field', 'required' );
+ add_filter(
+ 'woocommerce_default_address_fields',
+ function ( $fields ) {
+ $fields['phone']['required'] = false;
+ $fields['phone']['label'] = 'Mobile number';
+ return $fields;
+ }
+ );
+
+ $fields = ( new WC_Countries() )->get_address_fields( 'US', 'billing_' );
+
+ $this->assertFalse( $fields['billing_phone']['required'], 'The default address fields filter should make the billing phone optional.' );
+ $this->assertSame( 'Mobile number', $fields['billing_phone']['label'], 'The default address fields filter should rename the billing phone.' );
+ }
+
+ /**
+ * @testdox 'get_address_fields' applies the country locale to the billing phone, as it does to the shipping phone.
+ */
+ public function test_get_address_fields_applies_country_locale_to_billing_phone(): void {
+ update_option( 'woocommerce_checkout_phone_field', 'required' );
+ add_filter(
+ 'woocommerce_get_country_locale',
+ function ( $locale ) {
+ $locale['AL']['phone'] = array( 'hidden' => true );
+ return $locale;
+ }
+ );
+ $countries = new WC_Countries();
+
+ $billing = $countries->get_address_fields( 'AL', 'billing_' );
+ $shipping = $countries->get_address_fields( 'AL', 'shipping_' );
+
+ $this->assertTrue( $billing['billing_phone']['hidden'] ?? false, 'The locale should hide the billing phone.' );
+ $this->assertTrue( $shipping['shipping_phone']['hidden'] ?? false, 'The locale should hide the shipping phone.' );
+ }
+
/**
* @testdox 'get_formatted_address' outputs translated state names stored as HTML entities as text, including in uppercase.
*/