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

浪尽此生 提交于 2021-02-10 07:34:52

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/66017177/woocommerce-set-country-by-default-in-checkout-page-for-unlogged-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!