How to remove specific country in WooCommerce

Can someone tell how can I remove the specific country from woocommerce. There is option in woocommerce that said selling locations with All countries and specific.

However I want to sell to all countries except the 1 country that is US for example! then how can I remove US from the countries list. As if I use “specific countries” option then I will have to add all the countries except the US which is longer process.

Read More

Is there any code you can help me with that I can put into functions of theme so that US country will not appear in the list of countries during checkout?

Related posts

Leave a Reply

2 comments

  1. If you want to keep an array of countries, but you only have the keys, do something like this:

    function woo_remove_specific_country( $countries ) {
    
        global $woocommerce;
    
        // default do not limit
        $limited_countries = array_values(array_flip($countries));
    
        // Country array to keep
        $eu = $woocommerce->countries->get_european_union_countries( );
    
        // keep countries, sort them out of the full array to keep the names
        $found   = array_filter($countries, function($item) use ($eu) {
            return in_array($item, $eu);
        }, ARRAY_FILTER_USE_KEY); // USE_KEY is essential cause we are filtering the language codes
    
        // return the found countries
        return $found;
    }
    add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );