We’ve already seen how to set the default values for certain dropdowns (billing country and state, precisely). But what can we do when we want to define the defaults for the other “text” inputs, such as the billing_city?
PHP Snippet: Set the Default Billing City @ WooCommerce Checkout
/**
* @snippet WooCommerce Set Default City @ Checkout
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_checkout_fields', 'bbloomer_set_checkout_field_input_value_default' );
function bbloomer_set_checkout_field_input_value_default($fields) {
$fields['billing']['billing_city']['default'] = 'London';
return $fields;
}
How to Set the Default Value for Other WooCommerce Checkout Fields
Of course you can set the default for many more fields, just use the following instead of “[‘billing_city’]” inside the snippet:
- billing_first_name
- billing_last_name
- billing_company
- billing_address_1
- billing_address_2
- billing_city
- billing_postcode
- billing_country
- billing_state
- billing_email
- billing_phone
- shipping_first_name
- shipping_last_name
- shipping_company
- shipping_address_1
- shipping_address_2
- shipping_city
- shipping_postcode
- shipping_country
- shipping_state
como integro dentro de esos campos una variables que obtengo de una consulta proveniente de una base de datos?.
Hey Francisco, 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!
Hi Rodolfo!
Thanks for this amazing blog.
Question:
How do I set a default city with no option to change it? (I want it to be “read only”)
Thanks a lot!
Shmuel.
Hi Samuel, 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!
I want an automated email regarding city selection in the cart by user to the specified person for that city.
Can you help it?
Wajid, 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!
Hi Rodolfo Melogli
how can I add city and country to the United Arab Emirates ?
I can not add anything !
I need video tutorials
https://s8.picofile.com/file/8338374084/United_Arab_Emirates.png
Hey Abdul, thanks for your comment 🙂 Did you use my snippet?
Awesome.
How about setting default product data fields for virtual and downloadable products checkbox?
Any snippets or point in the right direction, thanks.
Hello Sam – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Dear Rodolfo,
Many thanks for your good tip. I have a problem about your article that I want to explain your in detail.
I want to re-order and also remove some fields in checkout page. At first, I used from woo checkout manager and also yith plugin to do that. They had strange problem: when I removed some fields, in checkout page, I got this error: “please enter an address to continue…” I had searched for that and finally I understood that when I removed billing_country field, it causes to error (because behind the scene it didn’t fill out). So I tried to set default value for country and then remove it by codes (or also plugin) but when I have used codes that you have mentioned above, in checkout page, billing_country fields remove and everything works to do checkout process but I have extra field that shows abbreviation of country name. This code in above that you explain it, defines default country but it didn’t have correct showing when you want to remove billing_country.
I appreciate if you revise this code.
Many thanks for your good article
BR
Mehdi
Mehdi, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Cool tip, it works.
I’ve got a question, If I want to have dropdown options of cities (like state/county) in place of text field, how do I do that ?
Thank you Abhi! I suggest you change the text field into a select box by looking at this useful blog from WooCommerce itself: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/. Let me know!