Custom sorting for WooCommerce checkout page country dropdown

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.

Share your thoughts

Your email address will not be published. Required fields are marked *