Search code examples
phpwordpresswoocommercedefaultcheckout

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


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?


Solution

  • 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