skip to Main Content

I have this function for hiding some shipping countries from the WooCommerce dropdown in the Checkout based on WPML language that is currenly active.

This code seems to be working but not 100%. This is my code so far:

if(ICL_LANGUAGE_CODE == 'nl') { 
    function woo_remove_specific_country( $country ) 
    {
        unset($country["DE"]);
        unset($country["AT"]);
        return $country; 
    }
    add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );
}   

This is almost working but the labels in the dropdown are empty, not gone completely.
See this screenshot: [Empty labels country dropdown][1]

Anybody knows how I can remove the empty labels too?
[1]: https://imgur.com/a/mUTpVar

2

Answers


  1. To completely remove the entries, you should not only unset the countries but also reindex the array after unsetting.

    if (ICL_LANGUAGE_CODE == 'nl') { 
        function woo_remove_specific_country($countries) 
        {
            $countries_to_remove = array("DE", "AT");
    
            foreach ($countries_to_remove as $country_code) {
                if (isset($countries[$country_code])) {
                    unset($countries[$country_code]);
                }
            }
    
            $countries = array_values($countries);
    
            return $countries; 
        }
        add_filter('woocommerce_countries', 'woo_remove_specific_country', 10, 1);
    }
    

    This should resolve the issue of empty labels in the country dropdown. Make sure to replace "DE" and "AT" with the actual country codes you want to remove. Additionally let me know if you have any issues.

    Login or Signup to reply.
  2. Use always the IF statement inside your function (and disable for backend)…
    Try the following revised code:

    add_filter('woocommerce_countries', 'conditionally_remove_specific_country', 10, 1);
    function conditionally_remove_specific_country( $country ) 
    {
        if ( ICL_LANGUAGE_CODE !== 'nl' || is_admin() ) {
            return $country;
        }
    
        if ( isset($country["DE"]) ) {
            unset($country["DE"]);
        }
    
        if ( isset($country["AT"]) ) {
            unset($country["AT"]);
        }
        return $country; 
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search