2

How to make the country default for regular (unregistered users). But if the buyer has a personal account and entered the country there, he would not be thrown into default in checkout?

I have tried to use WooCommerce: Set country by default in checkout page answer, but it does work for all users logged in and guest…

How to set the default country only for unregistered users?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Review: So, what did YOU try, and what was the result/error message? This might help answering the question. – H.Hasenack Feb 02 '21 at 20:33

1 Answers1

4

Use is_user_logged_in() conditional tag as follows:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country( $default ) {
    if ( ! is_user_logged_in() ) {
        $default = null;
    }
    return $default;
}

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

Related: WooCommerce: Set country by default in checkout page

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399