Developer Advisory: Update locale with checkout field changes

Issue overview

We recently had reports of shoppers being able to check out on some sites without paying for shipping. After investigating, we narrowed the scope of this issue down to sites modifying the core checkout fields using either: woocommerce_checkout_fields, woocommerce_shipping_fields, or woocommerce_billing_fields without updating the locale or woocommerce_default_address_fields filter to match. This led to a mismatch in configurations, essentially creating two conflicting sources of truth.

What you should be doing

When modifying the core checkout fields, ensure the default locale, and/or the locale of each country is also updated to match.

E.g. if you make postcode an optional field using woocommerce_checkout_fields then you should also use woocommerce_get_country_locale and woocommerce_get_country_locale_default to update the postcode field for all countries as well as the default locale.

You can also use woocommerce_default_address_fields if you can’t update locale for some reason, but the preferred and most supported method is modifying locale.

Example

⚠️ Previous code

// Make postcode optional in checkout fields
add_filter( 'woocommerce_checkout_fields', 'modify_postcode_field' );
function modify_postcode_field( $fields ) {
    // Make postcode optional in shipping fields
    if ( isset( $fields['shipping']['shipping_postcode'] ) ) {
        $fields['shipping']['shipping_postcode']['required'] = false;
    }
     
    // Make postcode optional in billing fields
    if ( isset( $fields['billing']['billing_postcode'] ) ) {
        $fields['billing']['billing_postcode']['required'] = false;
    }
     
    return $fields;
}

Updated code

add_filter( 'woocommerce_get_country_locale', 'make_postcode_optional' );
function make_postcode_optional( $locales ) {
    foreach( $locales as $country => $locale ) {
        $locales[$country]['postcode']['required'] = false;
    }
    return $locales;
 
}
 
add_filter( 'woocommerce_get_country_locale_default', 'make_postcode_optional_default' );
function make_postcode_optional_default( $locale ) {
    $locale['postcode']['required'] = false;
    return $locale;
}

Note, this will make the postcode optional in both shipping and billing. If you do want to change it in only one address type, please use the woocommerce_default_address_fields filter.

How can developers tell if they are affected?

If you:

  • Are using the shortcode checkout
  • Have used a plugin, or custom code snippet to hide checkout fields using woocommerce_checkout_fieldswoocommerce_shipping_fields, or woocommerce_billing_fields without updating the locale or woocommerce_default_address_fields
  • Are able to place orders without a shipping option being selected

What action do developers need to take if affected?

Update plugins/custom code snippets that hide checkout fields using woocommerce_checkout_fieldswoocommerce_shipping_fields, or woocommerce_billing_fields to also update the locale or woocommerce_default_address_fields.


Leave a Reply

Your email address will not be published. Required fields are marked *