How to Get WooCommerce Checkout Country Dropdown

WooCommerce Checkout Country DropdownWhen you are customizing the WooCommerce checkout page, you may want to get WooCommerce checkout country dropdown to change how the countries are displayed or limit the number of countries that are displayed in cases where you sell to specific countries.

In this quick  post, I am going to highlight how you can get the country dropdown at the checkout page and customize the display in your code to show the countries as you wish.

WooCommerce Checkout Country Dropdown

First you need to get the countries object at the checkout and this will have all the data that you need to display. You can achieve this using the code snippet below :

global $woocommerce;
$countries_object  =   new WC_Countries();
$countries         =   $countries_object->__get('countries');

You can now use the woocommerce_form_field() function to get the country form field as in the code snippet below :

    woocommerce_form_field('the_country_field', array(
    'type'       => 'select',
    'class'      => array( 'njengah-dropdown' ),
    'label'      => __('Select a country'),
    'placeholder'    => __('Enter something'),
    'options'    => $countries
    )
    );

As you can see in the options you need to pass the countries array obtained earlier by accessing the object created in the first code snippet.

The following is the complete code that you can use to get the countries select dropdown :

global $woocommerce;
    $countries_object   = new WC_Countries();
    $countries   = $countries_object ->__get('countries');

woocommerce_form_field('the_country_field', array(
    'type'       => 'select',
    'class'      => array( 'njengah-dropdown' ),
    'label'      => __('Select a country'),
    'placeholder'    => __('Enter something'),
    'options'    => $countries
    )
 );

Conclusion

In this post, I have briefly shared with you how you can get the countries select dropdown at the checkout and change how it is displayed on your checkout page.  If you would like help in customizing this further, please do not hesitate to get in touch with me

Similar Articles