Making woocommerce checkout form state field not required based on selected country

Solution:1

In case someone stumbles on this, Woocommerce changed the name of the state fields. Below is the updated solution:

add_filter( 'woocommerce_checkout_fields','custom_override_default_address_fields' );

function custom_override_default_address_fields($fields){
    global $woocommerce;
    $country = $woocommerce->customer->get_country();

    if($country !== 'US'){
        $fields['billing']['billing_state']['required'] = false;
        $fields['shipping']['shipping_state']['required'] = false;
    }

    return $fields;
}

Solution:2

Try this (in your functions.php)

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
     global $woocommerce;
     $country = $woocommerce->customer->get_country();
     if($country !== 'US'){
        $address_fields['state']['required'] = false;
     }
     return $address_fields;
}

This will make the ‘state’ field optional when the visitor is not from USA.
Hope this helps.

UPDATE

add_filter( 'woocommerce_checkout_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields($fields){
    global $woocommerce;
    $country = $woocommerce->customer->get_country();
    if($country !== 'US'){
        $fields['billing']['state']['required'] = false;
        $fields['shipping']['state']['required'] = false;
    }
    return $fields;
}