WooCommerce: Change default country on the cart page

I’d like to change the default country on the cart page to none, i.e. to have “Select a country…” appear as the default in the drop-down list of countries.

I’ve looked at this WooCommerce Docs snippet which uses the default_checkout_country filter but it only seems to apply to the checkout page (and not the cart page), as its title says. I also tested this by surrounding the apply_filters('default_checkout_country', ... in the files where they appear with echo statements and they don’t appear anywhere in the generated markup for the cart page.

Read More

As we are using PayPal Express Checkout we don’t use the WooCommerce built-in checkout page so it appears that the above mentioned snippet doesn’t apply and another solution is needed.

My next step was to look into woocommerce/templates/cart/shipping-calculator.php where the country option list is generated on the cart page. There’s a call to $woocommerce->customer->get_shipping_country(). That function in woocommerce/classes/class-wc-customer.php simply picks up the $_SESSION['customer']['shipping_country'] variables. The only place those variables are set is in the function set_shipping_to_base() in the same file and the only place that function is (conditionally) called is in woocommerce/shortcodes/shortcode-cart.php which I assume generates the cart from the shortcode used on the autogenerated cart page. I added a echo statement to the block in that file where that function is called but nothing appeared in the markup, so I got stuck there.

The above investigation seems however to be somewhat of a red herring as the above mentioned session variables seem to actually be set by the Base Country/Region setting on the General tab, as it would vary accordingly when the $_SESSION variable got printed in a browser where cookies and cache had been previously deleted. So I’m kind of stuck here too. I’d like the Base Country/Region setting to remain the country set, but still have no default country unless the visitor actually has selected it.

Any ideas?

Related posts

Leave a Reply

2 comments

  1. I have same issue, but need to set country to US, here is how I fix it:

    add_action('woocommerce_add_to_cart' , 'set_country_befor_cart_page'); 
    
    function set_country_befor_cart_page(){
    
        WC()->customer->set_billing_country('US');
        WC()->customer->set_shipping_country('US');
    }
    

    Hope it will help.

  2. By chance and just plain putzing around, I’ve come up with this:

    add_filter( 'woocommerce_checkout_fields' , 'default_country_fields' );
    
    function default_country_fields( $fields ) {
    
     $_SESSION['customer']['country'] = 'Select a country...';
     $_SESSION['customer']['shipping_country'] = 'Select a country...';
    
     return $fields;
    }
    

    As you can see, I’m simply setting the $_SESSION variables as needed. I’m kind of a n00b at this so, I’m unaware yet if there are any negative repercussions to this method. And there’s probably a more appropriate hook or filter for this, but what I used I got from here:

    tutorial-customising-checkout-fields-using-actions-and-filters

    If you are not logged in or have no billing/shipping info set to your account, you will correctly see the default values for billing/shipping country. However, when logged in with a user account that has billing or shipping info set, this function will be overridden and the defaults selected will reflect that of your user account billing info.