WooCommerce: Detecting Current User Country (Geolocation)
You may need to add some “personalization” to your WooCommerce website homepage. For example, you could print a custom greeting based on the user’s IP address.
For that, you can use PHP and WooCommerce inbuilt MaxMind Geolocation integration (when enabled from the General Settings Tab, of course).
Detecting the current user country could be very useful for other tasks, such as disabling payment gateways and shipping methods, printing country-specific content and so on. Enjoy!
An old version of the Business Bloomer website homepage featured a “personalized” greeting based on Geolocated User Country. I’ve since removed it, but the code is still valid!
PHP Snippet: Get Current User Country (Geolocation) To Display Conditional Content – WooCommerce
/**
* @snippet Get Current User Country - WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 7
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( '_______', 'bbloomer_use_geolocated_user_country' );
function bbloomer_use_geolocated_user_country(){
// Geolocation must be enabled @ Woo Settings
$location = WC_Geolocation::geolocate_ip();
$country = $location['country'];
// Lets use the country to echo greetings
switch ( $country ) {
case "IE":
$hello = "Howya!";
break;
case "IN":
$hello = "Namaste!";
break;
default:
$hello = "Hello!";
}
echo $hello;
}
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: Hide Products Based on IP Address / Geolocation There are many plugins that would allow you to do this in bulk or that would give you advanced features… but what if you just need to hide one product ID for users who are visiting your WooCommerce website from a specific country code? This is also called “geolocation” – remember this won’t work unless […]
WooCommerce: How to Test Geolocation? Great question, isn’t it? How do you make sure your shipping, taxes and currency settings are working properly when they depend on geolocation and you can only test from a single world location? WooCommerce uses a free geolocation service called MaxMind. By default this is active via the settings but can be disabled. The set […]
WooCommerce: How to Enable Multi-Currency? If you got here it’s because you’re looking to increase your sales conversion rate by enabling “Multi-Currency” in your WooCommerce website. The concept behind the currency switcher is simple: if I’m based in Europe I’d love paying in Euros, while if I were based in US I wouldn’t fancy the same treatment and I’d prefer […]
WooCommerce: How to Improve Product Search Personalized search and product navigation have a dramatic impact on ecommerce sales conversions, and for that reason alone, you should include them in your WooCommerce website build. But how do you improve the standard search function model included with WooCommerce? Well, reducing search times and quickly matching your customers to products are two great examples […]
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
36 thoughts on “WooCommerce: Detecting Current User Country (Geolocation)”
logan yu
Hi
Does maxmind only have country data, no state data?
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!
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!
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 ๐
Hi Rob,
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 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
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
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!
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.
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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you!
Hi
Does maxmind only have country data, no state data?
Thanks
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?
Thanks
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
Sure
Newbie here.
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?
Thanks
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 ๐
Hi Rob,
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.
Thanks.
Hey there ๐ It works on 3.5.3. so I don’t know, sorry
Hello,
Can is possible to do with region of France ?
Thanks
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!
Thank you!!
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 ๐
Hi Rodolfo.
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”? ๐