You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
Does this snippet (still) work?
Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
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.
36 thoughts on “WooCommerce: Detecting Current User Country (Geolocation)”
Does maxmind only have country data, no state data?
I think that’s correct
Going through all Your snippets has helped me get a lot of customizations I thought were only possible with plugins, and some I’ve never thought of doing. Thank Youn 100% much. Please I have an issue, I have a multivendor site and I want to set real-time shipping rates i.e Make Customers pay for shipping as per where the shop they are buying from is located. Is this possible?
Hi Jason, 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!
Can I modify this to get the checkout and cart pages to chose the country from geolocation?
You can already do that via the WooCommerce settings
Hi, thanks for sharing this!
However, at least to me it’s not clear how to use it, I mean, where this message is going to be visualized by default?
For example, if I want to visualize in the WooCommerce product page “Free Shipping to [country]”, what’s the process?
Hi Sanders, 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!
will this function work in 2019…will try
How can we use the same function twice in functions.php file?
I have some products that I want to show in Canada and some want to show in the USA. Is this achievable?
Sure Jack, you’d just use IF / THEN / ELSE in PHP
Doesn’t seem to work at all with my installation of Woo Commerce 3.5.4 🙁
Hello Bob, thanks for your comment! It works on the homepage of this very same website, so there must be something else you need to troubleshoot. Let me know 🙂
I’m using Woocommerce version 3.4.4 and I apply the code, but $location = WC_Geolocation::geolocate_ip() returns an empty array. Does version 3.4.4 able to use WC_Geolocation?
My problem is I must to use version 3.4.4, I can’t upgrade to higher version of Woocommerce.
Hey there 🙂 It works on 3.5.3. so I don’t know, sorry
Can is possible to do with region of France ?
Hey Jonas, 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
how about redirect language by specific country, ex: if user from germany language is germany, other than that language is english
Hey there 🙂 Your translation plugin should have that in the settings – check that out 🙂
Hey Rodolfo. Would it be possible to also echo the visitors country and possibly a flag?
Hey Danny, 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. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Great Rodolfo, I’ll try this code to make catalog mode in a specific country!
Brilliant 🙂 Ciao!
Nice snippet! Grazie mille! In Spanish, we could also say “¿Qué tal?”. Maybe a little bit more specific, not that generic. More or less like your “Come va?”. Ah! Tutto suona bellisimo in italiano!
Jaja, genial! Thank you Alberto 🙂
Great snippet, thank you so much.
I’m for Australia and I see “Ow ya goin’!” – it’s great. We also say “G’day, mate”.
Ahah brilliant! Does “mate” include ladies as well? That was my only worry 🙂
I’m ok with “Yo.” Some may consider it to be a very super casual street “hello” and some older folks may think it a bit too casual or not familiar, though.
🙂 I will try with “Hey!”
If you implement this as a shortcode then it will be easy to use anywhere you want.
Indeed, Damien! That’s what I’ve done on my homepage 🙂 Thanks for your input!
how to implement this code as shortcode?
It’s not too difficult, take a look here https://developer.wordpress.org/reference/functions/add_shortcode/
Hey Rodolfo! I see “Yo” here in the “YoU-nited States” .
Ahah brilliant! Would you prefer something different than “Yo”? 🙂