Make US first in Woocommerce checkout countries select field

Tags: , , , ,

I already have US selected as my default country in the woocommerce checkout. In addition to that, I was asked to move ‘US’ to the very top of the country list in the checkout form.

I created a new filter and hooked into ‘woocommerce_countries’ hook like this:

function change_country_order_in_checkout_form($countries)
    $countries = array('US' => $countries['US']) + $countries;

    return $countries;

add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );

My list of countries gets modified correctly, but then something in WooCommerce sorts the countries alphabetically and I want to avoid that. I tried adding:

remove_filter('woocommerce_sort_countries', 'wpautop');

but that did not seem to make any difference. Any help is appreciated.


To avoid ordering, you need to use woocommerce_sort_countries filter hook this way:

add_filter('woocommerce_sort_countries', '__return_false');

And to set “US” first, try this instead:

add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );
function change_country_order_in_checkout_form($countries)
    $usa = $countries['US']; // Store the data for "US" key
    unset($countries["US"]); // Remove "US" entry from the array

    // Return "US" first in the countries array
    return array('US' => $usa ) + $countries;

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Source: stackoverflow