Last active
          July 2, 2020 05:22 
        
      - 
      
- 
        Save rynaldos-zz/810b70068c087b7abc788b0b2541c546 to your computer and use it in GitHub Desktop. 
    [WooCommerce 3.0+] Change the default state and country on the checkout
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' ); | |
| add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' ); | |
| function change_default_checkout_country() { | |
| return 'XX'; // country code | |
| } | |
| function change_default_checkout_state() { | |
| return 'XX'; // state code | |
| } | |
| // Did this help? Donate me some BTC: 1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG | 
There is issue with the above code. If the user has saved billing country in his/her account, in checkout the billing country will be resetted to the country mentioned in the above filter i.e. the default country rather than user's saved billing country.
Add a condition to the function, to check if the user already exists, like so:
function change_default_checkout_country( $country ) {
    // If the user already exists, don't override country
    if ( WC()->customer->get_is_paying_customer() ) {
        return $country;
    }
    return 'country_code'; // override to default country
}
Cheers!
any update for 'SHIP TO A DIFFERENT ADDRESS?' default country set?
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This also does not update the default country used when estimating the cost to ship on the cart page. How can that be set to a default country?