Create a gist now

Instantly share code, notes, and snippets.

Embed
WooCommerce - Set default state/country for checkout
/**
* Manipulate default state and countries
*
* As always, code goes in your theme functions.php file
*/
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'XX'; // country code
}
function change_default_checkout_state() {
return 'XX'; // state code
}
@gizburdt

This comment has been minimized.

Show comment
Hide comment
@gizburdt

gizburdt Oct 23, 2012

This does't work for me. I'm using this piece of code:

add_filter( 'default_checkout_country', 'mhxl_default_checkout_country' );
function mhxl_default_checkout_country()
{
return 'NL';
}

This does't work for me. I'm using this piece of code:

add_filter( 'default_checkout_country', 'mhxl_default_checkout_country' );
function mhxl_default_checkout_country()
{
return 'NL';
}

@point4design

This comment has been minimized.

Show comment
Hide comment
@point4design

point4design Oct 23, 2012

This doesn't work for me either. Here is the code I'm using:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );

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

Please help

This doesn't work for me either. Here is the code I'm using:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );

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

Please help

@smfacer

This comment has been minimized.

Show comment
Hide comment
@smfacer

smfacer Oct 25, 2012

Not working here either with:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );

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

smfacer commented Oct 25, 2012

Not working here either with:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );

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

@mattowc

This comment has been minimized.

Show comment
Hide comment
@mattowc

mattowc Oct 26, 2012

Possible solution for those having an issue above. If you are logged into WordPress, WooCommerce will set the default country and state to what is available from your profile. If they are not set, it will ask you to choose one. Either fill out your profile details or logout and you will see the correct default country.

  • Jon

mattowc commented Oct 26, 2012

Possible solution for those having an issue above. If you are logged into WordPress, WooCommerce will set the default country and state to what is available from your profile. If they are not set, it will ask you to choose one. Either fill out your profile details or logout and you will see the correct default country.

  • Jon
@jmbullis

This comment has been minimized.

Show comment
Hide comment
@jmbullis

jmbullis Mar 3, 2013

Thanks mattowc. I checked in a different different browser and it was working just fine.

jmbullis commented Mar 3, 2013

Thanks mattowc. I checked in a different different browser and it was working just fine.

@gypsydo

This comment has been minimized.

Show comment
Hide comment
@gypsydo

gypsydo Feb 10, 2014

"Possible solution for those having an issue above. If you are logged into WordPress, WooCommerce will set the default country and state to what is available from your profile. If they are not set, it will ask you to choose one. Either fill out your profile details or logout and you will see the correct default country."

I am still having the issue of a logged-in user not having the country auto-populated, even though their profile shows that they have a country selected. Advice?

gypsydo commented Feb 10, 2014

"Possible solution for those having an issue above. If you are logged into WordPress, WooCommerce will set the default country and state to what is available from your profile. If they are not set, it will ask you to choose one. Either fill out your profile details or logout and you will see the correct default country."

I am still having the issue of a logged-in user not having the country auto-populated, even though their profile shows that they have a country selected. Advice?

@WilCF

This comment has been minimized.

Show comment
Hide comment
@WilCF

WilCF Feb 12, 2014

This does not work for me - people can only get shipped to Washington, state, our base state - are there any caches we/they need to clear? Browsers they can not use, etc.?

WilCF commented Feb 12, 2014

This does not work for me - people can only get shipped to Washington, state, our base state - are there any caches we/they need to clear? Browsers they can not use, etc.?

@matteoraggi

This comment has been minimized.

Show comment
Hide comment
@matteoraggi

matteoraggi Oct 29, 2015

Great! It works with woocommerce 2.4.7!
I added the default city with this snippet too:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

function custom_override_default_address_fields( $address_fields ) {
$address_fields['city']['placeholder'] = 'Gorizia';
return $address_fields;
}

Great! It works with woocommerce 2.4.7!
I added the default city with this snippet too:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

function custom_override_default_address_fields( $address_fields ) {
$address_fields['city']['placeholder'] = 'Gorizia';
return $address_fields;
}

@aibidubey

This comment has been minimized.

Show comment
Hide comment
@aibidubey

aibidubey Jan 19, 2016

Great its Working :)

Great its Working :)

@sibiant

This comment has been minimized.

Show comment
Hide comment
@sibiant

sibiant Apr 27, 2017

Is there anyway to replace 'Select an option' with a state name from woocommerce checkout page (billing_state field). Also needed to show other STATES when click the select box.

(I wanted to set a default state in checkout form and other states will only when click the drop down select box.)

sibiant commented Apr 27, 2017

Is there anyway to replace 'Select an option' with a state name from woocommerce checkout page (billing_state field). Also needed to show other STATES when click the select box.

(I wanted to set a default state in checkout form and other states will only when click the drop down select box.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment