[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Woocommerce how to set default_checkout_billing_country only if user is not logged in

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9463
    isak-la-fleur
    Participant

    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'; 
    }
    
    #9464
    ruvee
    Participant

    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!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.