Based on How to remove all Woocommerce checkout billing fields without errors answer code, this is my code attempt:
add_action( 'woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5 );
function hide_checkout_billing_country() {
echo '<style>#billing_state_field{display:none !important;}</style>';
}
add_filter('woocommerce_billing_fields', 'customize_checkout_fields', 100 );
function customize_checkout_fields( $fields ) {
if ( is_checkout() ) {
// HERE set the required key fields below
$chosen_fields = array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'postcode', 'country', 'state');
foreach( $chosen_fields as $key ) {
if( isset($fields['billing_'.$key]) && $key !== 'state') {
unset($fields['billing_'.$key]); // Remove all define fields except country
}
}
}
return $fields;
}
My goal is to hide the state field depenging on country. Eg.
- If country = Italy then the state field is shown
- If country = France then state field is hidden..
So I tried to insert an if condition but without the desired result.
If someone could give me advice, it would be very appreciated.