Ad
Woocommerce How To Set Default_checkout_billing_country Only If User Is Not Logged In
I have this function and I only want to set the billing country if user is a guest, otherwise we should not change the billing country, but instead take the billing country as what the user has in his profile in WooCommerce.
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'US';
}
Ad
Answer
You could use is_user_logged_in
:
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country($default) {
if(is_user_logged_in()){
return $default;
}else{
return 'US';
};
};
However, sometimes, because of caching issues, is_user_logged_in
doesn't work. In that case then you could use global $current_user
.
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country($default) {
global $current_user;
if($current_user->ID){
return $default;
}else{
return 'US';
};
};
Let me know if you could get it to work!
Ad
source: stackoverflow.com
Related Questions
- → CORS missmatch because of http
- → Building sitemap for 2 wordpress install under 1 domain
- → How to remove empty elements after class?(jQuery)
- → Get width of an element and apply to another relative to the first one?
- → How to remove caption p class from wordpress?
- → 301 Redirection from no-www to www in wordpress
- → Laravel 5 routing using prefix
- → WordPress - Header position Top, Left &Right
- → how to add rel=nofollow to some specific external links in wordpress
- → octobercms install error: database is not empty?
- → How to edit the index page of wordpress theme?
- → How to select a Post Type (Wordpress) to pass a filter in head?
- → What sort of URL structure should be used to display AMP HTML vs vanilla HTML
Ad