I am building my WooCommerce store and need to alter the checkout billing/shipping fields. Currently, it displays the state/county as optional for Portugal country, but it is important for me that it is required and marked with the red asterisk because this affects prices.
I have tried plugins like Checkout Field Editor for WooCommerce and others, but I have had no success. Have also tried some code I found online which I added to functions.php on child theme:
add_filter( 'woocommerce_billing_fields', 'ts_require_wc_state_field');
function ts_require_wc_state_field( $fields ) {
$fields['billing_state']['required'] = true;
return $fields;
}
and
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_address_fields');
function custom_override_default_address_fields( $address_fields )
{
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
Both code snippets didn't work.
Perhaps someone can help me out with the right code for child theme?
A bit of a novice at this, so any help is really appreciated.
For some countries, like Portugal, it requires something different to work. Try the following:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works with the code below.
But as states are not defined in WooCommerce for Portugal, It will require you to define each state name (with its related state code) in the following function:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
You will get something like:
and:
Related: Make state checkout field required for a Vietnam country in WooCommerce