By default, all countries in the checkout drop-down field are sorted alphabetically, but if you’d like to change the WooCommerce country select dropdown, and show US or UK at the very top, then the following code will help. It sorts the checkout page country dropdown in a way you’d like it to show.
add_filter( 'woocommerce_sort_countries', '__return_false' ); add_filter( 'woocommerce_countries', 'wc_custom_countries_order', 10, 1 ); function wc_custom_countries_order( $countries ) { // replace with iso code of the country (example: US or GB) unset($countries['country_1_iso_code']); unset($countries['country_2_iso_code']); unset($countries['country_3_iso_code']); // replace with iso code of country AND country name (example: US | United States or GB | United Kingdom (UK) $countries = ['country_1_iso' => 'country_1_name'] + ['country_2_iso' => 'country_2_name'] + ['country_3_iso' => 'country_3_name'] + $countries; return $countries; }
Was this helpful? Please buy me a coffee
This code should be added to your child theme’s
functions.php
file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php
file as this will be wiped entirely when you update.
this is not working please tell how to set country key value or iso value in this code
Howdy!
The country_1_iso_code would be the ISO code for the country, as in US, or GB etc.
The country_1_name would be the name of the country, matching the ISO code, as in United States, or Britain, etc.
Hi,
Thanks for this!
Any idea how I can get in a “separator” in there? So, like:
Country 1
Country 2
—
Country 3
Country 4
Country 5
I tried adding the separator as a country itself but I can’t set it to read-only without using JavaScript.
Try something like:
['country_1_iso' => 'country_1_name'] + ['—'] + ['country_2_iso' => 'country_2_name'] + ['country_3_iso' => 'country_3_name'] + $countries;
This code is not working in latest woocommerce. TO be specific this filter is not working
add_filter( ‘woocommerce_sort_countries’, ‘__return_false’ );
Other parts of the code works.