WooCommerce: Set country by default in checkout page

11,428

Solution 1

Update (Since WooCommerce 3)

This are the woocommerce hooks and code to be used for this purpose for country (and optionally state):

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_country_and_state' );
function change_default_checkout_country_and_state( $default ) {
    return null;
}

Or even shorter:

add_filter( 'default_checkout_billing_country', '__return_null' );
add_filter( 'default_checkout_shipping_country', '__return_null' );
add_filter( 'default_checkout_billing_state', '__return_null' );
add_filter( 'default_checkout_shipping_state', '__return_null' );

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

Note: default_checkout_country and default_checkout_state hooks are deprecated and replaced since WooCommerce 3

Related: WooCommerce: Set country by default in checkout page for unlogged users

Solution 2

It's worth noting that the most current method for achieving this will likely always be viewable in the Woocommerce documentation, here.

It's also worth noting it may be undesirable to remove the default country for existing (logged-in) customers. At least on sites that allow customers to have a customer account. For those customers the address fields will be automatically filled out for them, and if you remove the default country for all uses, then their address will be filled out, except that the country will be missing.

With that in mind, as of January 2022, I would suggest this code as a useful way to achieve what the OP requested:

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 null; 
}

This is a slight variation of what's conveyed in the WC documentation, which was addressing how to change what the default country is set to. In this case we're setting it to Null.

Share:
11,428
Sonia Khan
Author by

Sonia Khan

Updated on July 05, 2022

Comments

  • Sonia Khan
    Sonia Khan almost 2 years

    I am using WooCommerce in my Wordpress web site. The customers billing and shipping details are populated by default on checkout page. I want that the country will not be set by default. Instead it will asked to select country even if user is logged in.

    Screenshot link

    Any suggestions? What I should do in order to achieve this?

  • imVJ
    imVJ almost 8 years
    @SoniaKhan /wp-content/themes/{youractivethemename}/functions.php