Home » Set Default Country And State On The Checkout Form In WooCommerce

Set Default Country And State On The Checkout Form In WooCommerce

Last updated on December 24, 2020 by

For better user experience, you should set default country and state on the checkout form in WooCommerce. Are you ready? Let’s do it together.

Table of Contents
1. An Ideal Place to Add Code in WordPress
2. Set Default Country and State on Checkout Form
3. How to Get List of Country & State Code for WooCommerce?

To do so, we will use 'default_checkout_billing_country' and 'default_checkout_billing_state' filter hooks. As the hook’s name suggests, both will set the default country and state on the billing form of the checkout page.

Let’s set “Australia(AU)” as a default country and “New South Wales(NSW)” as a default state.

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please note that all the code has been tested with Storefront (Parent Theme) & Storefront-Child theme.

Set Default Country and State on The Checkout Form in WooCommerce

/**
 * Change the default state and country on the checkout page
 */

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

function change_default_checkout_country() {
  return 'AU'; // country code
}

add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );

function change_default_checkout_state() {
  return 'NSW'; // state code
}

That’s it. Now, go to the checkout page and test the result. You will see the country dropdown now by default set to “Australia” and state dropdown set to “New South Wales”.

The above code will affect both existing and non-existing users. So if you want to only change the default for non-existing users, then you can use the following:

/**
 * Change the default country on the checkout for non-existing users only
 */

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country', 10, 1 );

function change_default_checkout_country( $country ) {

    // If the user already exists, don't override country
    
    if ( WC()->customer->get_is_paying_customer() ) {
        return $country;
    }

    return 'AU'; // Override default to Australia
}

How to Get List of Country & State Code for WooCommerce?

Are you tired of searching country code & state code for WooCommerce? Don’t worry, we have prepared a list of country and state code from the official WooCommerce plugin. Click to get the List of Country Code and State Code for WooCommerce.

Additionally read our guide, How to Redirect Users after Add to Cart in WooCommerce

That’s it from us. We hope this article helped you to set the default country and state on the checkout form in WooCommerce.

Please let us know in the comments if everything worked as expected, your issues or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

1 thought on “Set Default Country And State On The Checkout Form In WooCommerce”

Leave a Comment