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!

PHP Snippet: Get Current User Country (Geolocation) To Display Conditional Content – WooCommerce
/**
* @snippet Get Current User Country - WooCommerce
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
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;
}
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”? π