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
 * @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;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

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)

  1. Hi

    Does maxmind only have country data, no state data?

    Thanks

    1. I think that’s correct

  2. 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?

    1. 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!

  3. Can I modify this to get the checkout and cart pages to chose the country from geolocation?

    1. You can already do that via the WooCommerce settings

  4. 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

    1. 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!

  5. will this function work in 2019…will try

    1. Sure

  6. 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

    1. Sure Jack, you’d just use IF / THEN / ELSE in PHP

  7. Doesn’t seem to work at all with my installation of Woo Commerce 3.5.4 ๐Ÿ™

    1. 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 ๐Ÿ™‚

  8. 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.

    1. Hey there ๐Ÿ™‚ It works on 3.5.3. so I don’t know, sorry

  9. Hello,
    Can is possible to do with region of France ?
    Thanks

    1. 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

  10. how about redirect language by specific country, ex: if user from germany language is germany, other than that language is english

    1. Hey there ๐Ÿ™‚ Your translation plugin should have that in the settings – check that out ๐Ÿ™‚

  11. Hey Rodolfo. Would it be possible to also echo the visitors country and possibly a flag?

    1. 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

  12. Great Rodolfo, I’ll try this code to make catalog mode in a specific country!
    Thank you!!

    1. Brilliant ๐Ÿ™‚ Ciao!

  13. 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!

    1. Jaja, genial! Thank you Alberto ๐Ÿ™‚

  14. 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”.

    1. Ahah brilliant! Does “mate” include ladies as well? That was my only worry ๐Ÿ™‚

  15. 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.

    1. ๐Ÿ™‚ I will try with “Hey!”

  16. If you implement this as a shortcode then it will be easy to use anywhere you want.

    1. Indeed, Damien! That’s what I’ve done on my homepage ๐Ÿ™‚ Thanks for your input!

      1. how to implement this code as shortcode?

  17. Hey Rodolfo! I see “Yo” here in the “YoU-nited States” .

    1. Ahah brilliant! Would you prefer something different than “Yo”? ๐Ÿ™‚

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!

Your email address will not be published. Required fields are marked *