-
-
Save rynaldos-zz/810b70068c087b7abc788b0b2541c546 to your computer and use it in GitHub Desktop.
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 |
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/
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
}
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?
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?
How do I implement this? I tried to place it in functions.php and it does not work.