WooCommerce: Detecting Current User Country (Geolocation)

In the redesign of Business Bloomer homepage, launched recently, I wanted to add some “personalization” to the main headline. In detail, I wanted to make use of PHP and WooCommerce inbuilt MaxMind Geolocation (when enabled from the General Settings Tab of course) to print a custom greeting on the screen.

The detection of 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. And as usual, this “detection” is done with one line of code!

The new Business Bloomer homepage with a “personalized” greeting based on Geolocated User Country

PHP Snippet: Get Current User Country (Geolocation) – WooCommerce

 

/**
 * @snippet       Get Current User Country (Geolocation) - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=72811
 * @author        Rodolfo Melogli
 * @compatible    Woo 3.5.3
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 e.g. echo greetings

switch ($country) {
    case "IE":
        $hello = "Howya!";
        break;
    case "IN":
        $hello = "Namaste!";
        break;
    default:
        $hello = "Hello!";
}

echo $hello;
}

add_action( '_______', 'bbloomer_use_geolocated_user_country' );

Where to add this snippet?

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.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

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!

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.

34 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!

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

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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 :)

Your email address will not be published.