How to remove county field (or billing_state) on checkout if country is UK/GB?

I got what you need. Lost the original source to credit but use this code. Add this to your themes functions.php

add_filter( 'woocommerce_states' , 'keep_specific_country_states', 10, 1 );
function keep_specific_country_states( $states ) {
    // HERE define the countries where you want to keep
    $countries = array('US', 'AU', 'CA');
    $new_country_states = array();

    // Loop though all country states
    foreach( $states as $country_code => $country_states ){
        if( ! in_array( $country_code, $countries ) ){
            // Remove states from all countries except the defined ones
            $states[$country_code] = array();
        }
    }
    return $states;
}

Add countries that require a state to;

$countries = array('US', 'AU', 'CA');

I think its better to do it by JS.


// hide country field for UK
$(document.body).on("updated_checkout", function (e) {
    var sm_selected_country = $("#billing_country").val();
    console.log("selected county", sm_selected_country);
    if (sm_selected_country && sm_selected_country === "GB") {
        $("#billing_state_field").addClass("d-none");
    } else {
        $("#billing_state_field").removeClass("d-none");
    }
});
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.