2

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.

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

2

Your code contains some errors and shortcomings

  • The modification in your code attempt, based on the state does not apply to the country
  • Your code contains an Fatal error: "Uncaught Error: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function "customize_checkout_fields" not found or.. I've edited this though
  • You can use WC()->customer->get_billing_country() to get the billing country

So you get:

function filter_woocommerce_billing_fields( $fields ) { 
    // Returns true when viewing the checkout page
    if ( is_checkout() ) {
        // Get billing country
        $billing_country = WC()->customer->get_billing_country();
        
        // Multiple country codes can be added, separated by a comma
        $countries = array( 'IT' );
        
        // NOT in array
        if ( ! in_array( $billing_country, $countries ) ) {
            // HERE set the fields below, multiple fields can be added, separated by a comma
            $chosen_fields = array( 'state' );
            
            // Loop through
            foreach( $chosen_fields as $key ) {
                // Isset
                if ( isset( $fields['billing_' . $key] ) ) {
                    // Remove all defined fields
                    unset( $fields['billing_' . $key] );
                }
            }
        }
    }
    
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'filter_woocommerce_billing_fields', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Good evening 7uc1f3r, first of all thank You so much, for attention. My goal is to hide state field depenging on country. Eg. if country == Italy then state field is shown but if country == France then state field is hidden. I have tried to readjust the code shared above to achieve this in fact, as you can see I wrote `if( isset($fields['billing_'.$key]) && $key !== 'state') {` trying to hide it. I apologize for not being clear enough. Warm regards Ale – Alessandro Valori Oct 01 '21 at 18:44
  • 1
    @AlessandroValori I've updated my answer. It is indeed important to write clear questions, better too much information than too little information. This way, misunderstandings like this can be avoided – 7uc1f3r Oct 01 '21 at 19:09
  • Good morning, sorry for my late reply. I tried Your code but it doesn't work. The only one code that works is the following: function so_01_filter_woocommerce_states( $states ) { unset( $states['FR'] ); return $states; }; add_filter( 'woocommerce_states', 'so_01_filter_woocommerce_states', 10, 1 ); function so_01_filter_woocommerce_get_country_locale( $locale ) { $locale['LU']['state']['required'] = true; return $locale; }; add_filter( 'woocommerce_get_country_locale', 'so_01_filter_woocommerce_get_country_locale', 10, 1 ); but it works only for one single country – Alessandro Valori Oct 06 '21 at 12:07