Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active July 2, 2020 05:22
Show Gist options
  • Save rynaldos-zz/810b70068c087b7abc788b0b2541c546 to your computer and use it in GitHub Desktop.
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
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
@bobbywhitley
Copy link

How do I implement this? I tried to place it in functions.php and it does not work.

@lukecav
Copy link

lukecav commented Sep 5, 2017

So replace XX with the country code and the same with XX to the state code, with the defaults you want.
https://calebburks.com/correctly-add-custom-code-woocommerce/

@amyelzea
Copy link

amyelzea commented Nov 27, 2017

This seems to only change the Country/State default for Billing Details. If Ship to a different address? is selected - the Country/State in Shipping address is not updated to reflect the defaults listed in Billing.

Extra line of code to set default for Shipping address also?
Similar question for the Cart Shipping Calculator?

Code added:
/**

  • Changes default country on checkout.
    */
    add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );

    function change_default_checkout_country() {
    return 'US'; // country code
    }

checkout-defaults-edit

@jeremiahbenes
Copy link

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?

@manojmohangit
Copy link

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.

@rynaldos-zz
Copy link
Author

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!

@charls637
Copy link

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