WooCommerce: Set country by default in checkout page
Asked Answered
R

5

7

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?

Reconstruct answered 8/5, 2016 at 20:52 Comment(0)
B
6

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

Border answered 8/5, 2016 at 21:23 Comment(1)
@SoniaKhan /wp-content/themes/{youractivethemename}/functions.phpGeof
R
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.

Rosenwald answered 21/1, 2022 at 1:16 Comment(0)
E
0

Those function has been depreciated

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );

function change_default_checkout_country() {
    return null;
    //return get_user_meta( get_current_user_id() , 'billing_country', true ); // for retrun user saved country
}

function change_default_checkout_state() {
    return null;
    //return get_user_meta( get_current_user_id() , 'billing_state', true ); // for retrun user saved state
}

Tested in WooCommerce Version 4.5.2

Encase answered 28/9, 2020 at 4:50 Comment(0)
D
0

I used the following code in functions.php which is not working at all, any clues please?

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

function change_default_checkout_country() {
  return 'UK'; // country code
Declivity answered 12/10, 2021 at 18:14 Comment(0)
N
0

I just wanted to let you know that I have tackled the same issue.

I have written a small plugin here: https://github.com/TakesTheBiscuit/woocommerce_set_country

The primary user experience fix here is to provide a select box to users before they get to the cart or checkout - that way you can tailor your user experience elsewhere for their session - such as custom pricing, shipping rates, taxes etc.

Whilst writing the plugin I also found that admin (who you are usually logged in as a site owner) already often has a country set, so to test the functionality we want to exclude admins from having their default country.

function change_default_checkout_country($country) {

if (current_user_can( 'manage_options' )) {
    } else {
        if ( WC()->customer->get_is_paying_customer() ) {
            return $country;
        }
    }

    $returnCountry = '';
    if (strlen($_SESSION['wc_country_iso']) == 2) {
        $returnCountry = $_SESSION['wc_country_iso'];
    } else {
        $countries_obj   = new WC_Countries();
        $returnCountry = $countries_obj->get_base_country();
    }

    return $returnCountry;
}

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

Final thought from me; You'll also find the shipping calculator is not able to pick up these default countries, frustratingly - so you'll need to work around that too.

Nereid answered 25/1, 2022 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.