Make the State / County Field Optional
Stops requiring the state, county or province at checkout for every country, for stores whose couriers do not need it.
Opens WPIDE on
Code
add_filter( 'woocommerce_get_country_locale', function ( $locales ) {
foreach ( $locales as $country => $fields ) {
if ( isset( $fields['state'] ) ) {
$locales[ $country ]['state']['required'] = false;
}
}
return $locales;
}, 20 );
add_filter( 'woocommerce_get_country_locale_default', function ( $fields ) {
if ( isset( $fields['state'] ) ) {
$fields['state']['required'] = false;
}
return $fields;
}, 20 );
add_filter( 'woocommerce_default_address_fields', function ( $fields ) {
if ( isset( $fields['state'] ) ) {
$fields['state']['required'] = false;
}
return $fields;
}, 20 );
Notes
The field stays visible; customers can still fill it in.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.