Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: Set country by default in checkout page

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?

like image 636
Sonia Khan Avatar asked May 08 '16 20:05

Sonia Khan


People also ask

How do I change the default checkout page in WordPress?

From the WordPress Dashboard, go to WooFunnels > Settings. Here, go to the Checkouts tab. Select the Checkout page from the drop-down that you want to set as your default checkout page.


2 Answers

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

like image 91
LoicTheAztec Avatar answered Oct 30 '22 10:10

LoicTheAztec


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.

like image 45
inspirednz Avatar answered Oct 30 '22 09:10

inspirednz