WooCommerce: Set Default Billing State / Country @ Checkout
This is a simple PGP snippet that I successfully used on a bunch of websites in order to set the default Billing country or state. This is a great way to save the user some time during checkout (and therefore increase your conversion rate) in case you almost exclusively sell to customers located in a given country or state. Nothing else to say, just… enjoy!
PHP snippet: Set Default Billing Country or State @ WooCommerce Checkout Page
/**
* @snippet Set Default Billing Country @ WooCommerce Checkout Page
* @how-to Get CustomizeWoo.com FREE
* @source https://businessbloomer.com/?p=601
* @author Rodolfo Melogli
* @compatible Woo 3.5.4
* @community https://businessbloomer.com/club/
*/
// Example 1: default state to OREGON
add_filter( 'default_checkout_billing_state', 'bbloomer_change_default_checkout_state' );
function bbloomer_change_default_checkout_state() {
return 'OR'; // state code
}
// Example 2: default country to United States
add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );
function bbloomer_change_default_checkout_country() {
return 'US';
}
This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101
Related content
WooCommerce: Cart and Checkout on the Same Page This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
WooCommerce: Disable Payment Method If Product Category @ Cart Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
WooCommerce: Add Privacy Policy Checkbox @ Checkout Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
WooCommerce: Redirect to Custom Thank you Page How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
WooCommerce: Disable Payment Gateway by Country You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli
25 thoughts on “WooCommerce: Set Default Billing State / Country @ Checkout”
Kris
Can this snippet be generalized to set the default country when customers add new shipping or billing addresses? I have a plug-in that allows a customer to have multiple shipping addresses for WooCommerce. Whenever a customer creates a new address, I’d like to have the form default to the US. Is this easily done (assuming the plug-in is using the usual WooCommerce tables)?
when im using this, Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘change_default_checkout_state’ not found or invalid function name in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 289 , i set this filter in checkout page in plugin. i create plugin for override function woocommerce
function __construct()
{
//filter to empty checkout country and checkout state
add_filter('default_checkout_billing_country', 'change_default_checkout_country');
add_filter('default_checkout_billing_state', 'change_default_checkout_state');
}
for initiate filter in plugin file checkout
this is for function
function change_default_checkout_country()
{
return null;
}
/*
Add Destination's ID to POST parameter
@filter default_checkout_billing_state
@return mixed
*/
function change_default_checkout_state()
{
return null;
}
Hi Chris, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Sorry but just can’t get this to work at all. We have a website where the default country is Sweden, but we need to be able to set the checkout country to another. I used this code ‘as is’ to try and force it to the US, as an example, but the default country is still Sweden. Any ideas?
i can set default country if i not go to page chechout.
Example: i set default country “US” on page cart, and i get billing country on page cart, i not change
function default_checkout_city(){
return 'x'; // state code
}//add_filter( 'woocommerce_countries_base_city', 'default_checkout_city' );
add_filter( 'woocommerce_countries_base_city' , 'set_woocommerce_countries_base_city' );
function set_woocommerce_countries_base_city() {
// Replace with your store town/city
return 'x';
}
Hey there Jhonson, thanks for your comment! See, the city field behaves differently from the state ad country ones: it’s an input text field and not a select box. Today I felt generous though – here’s my brand new post which gives you the solution: https://businessbloomer.com/woocommerce-set-default-city-values-checkout π Thanks so much!
Hey Satish, thanks for your comment. I think you have something wrong in the WooCommerce settings π Can you send me a screenshot of your WooCommerce/Settings/General page? Cheers. ~R
Questions? Feedback? Customization? Leave your comment now! _____
If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!
Can this snippet be generalized to set the default country when customers add new shipping or billing addresses? I have a plug-in that allows a customer to have multiple shipping addresses for WooCommerce. Whenever a customer creates a new address, I’d like to have the form default to the US. Is this easily done (assuming the plug-in is using the usual WooCommerce tables)?
Thanks!
Yes, but pretty much it depends on how the plugin is coded. Ask their support team
when im using this, Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘change_default_checkout_state’ not found or invalid function name in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 289 , i set this filter in checkout page in plugin. i create plugin for override function woocommerce
for initiate filter in plugin file checkout
this is for function
Thanks
Thanks, works for me, just what I was looking for.
Great!
Hello, that dont work for me, i use this code directly on function.php ang default country dont change, can i have help ?
What code did you use?
Hello, can we set default state for countrys that woocomerce doesnt have stored state codes like Greece?
Hi Chris, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Sorry but just can’t get this to work at all. We have a website where the default country is Sweden, but we need to be able to set the checkout country to another. I used this code ‘as is’ to try and force it to the US, as an example, but the default country is still Sweden. Any ideas?
Hey James, thanks for your comment! Maybe you have geolocation enabled from the WooCommerce settings?
i can set default country if i not go to page chechout.
Example: i set default country “US” on page cart, and i get billing country on page cart, i not change
Hello Cheng, thanks for your comment. Unfortunately I don’t understand it fully, so maybe you could provide a screenshot? Thank you
Hi, do you how to set default city?
It’s not working for me. Tried
Hey there Jhonson, thanks for your comment! See, the city field behaves differently from the state ad country ones: it’s an input text field and not a select box. Today I felt generous though – here’s my brand new post which gives you the solution: https://businessbloomer.com/woocommerce-set-default-city-values-checkout π Thanks so much!
how to remove the default selected country i need to show ‘select country’, thanks
Hey Satish, thanks for your comment. I think you have something wrong in the WooCommerce settings π Can you send me a screenshot of your WooCommerce/Settings/General page? Cheers. ~R
thanks Rodolfo,
I found a solution by in this way,
add_filter( ‘default_checkout_country’, ‘change_default_checkout_country’ );
function change_default_checkout_country() {
return ”;
}
Awesome π
This is a great snippet, thanks.
Thanks so much for your feedback Debbie!
where about do you insert this code?
Hello Doug, you can do so in your active themeβs function.php file, or use Code Snippets free plugin. Hope this helps!