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 you’ve enabled geolocation inside the WooCommerce general settings. Other than that, get your product ID, find out the target country 2-letter code, and this snippet will do the trick. Enjoy!

Case study: hiding a given product ID to users based in a specific country (IT)

PHP Snippet 1: Hide Product ID Based on Geolocated Country @ WooCommerce Shop

In the example below, I’m hiding product ID = 344 if the current visitor is from Italy. This PHP Snippet may give you pagination problems sometimes – if this fails, check out snippet 2 below.

/**
 * @snippet       Hide Product Based on IP Address
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.0
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_is_visible', 'bbloomer_hide_product_if_country', 9999, 2 );

function bbloomer_hide_product_if_country( $visible, $product_id ){
	$location = WC_Geolocation::geolocate_ip();
	$country = $location['country'];
	if ( $country === "IT" && $product_id === 344 ) {
		$visible = false;
	}
	return $visible;
}

PHP Snippet 2 (Alternative): Hide Product ID Based on IP Address @ WooCommerce Shop

In this example, I’m hiding products 21 and 32 if the user is browsing from a USA IP address.

/**
 * @snippet       Hide Product Based on IP Address
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.0
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_product_query', 'bbloomer_hide_product_if_country_new', 9999, 2 );

function bbloomer_hide_product_if_country_new( $q, $query ) {
    if ( is_admin() ) return;
    $location = WC_Geolocation::geolocate_ip();
    $hide_products = array( 21, 32 );	
    $country = $location['country'];	
    if ( $country === "US" ) {
        $q->set( 'post__not_in', $hide_products );
    } 
}

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: 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 […]
  • WooCommerce: Add Upsell Area @ Checkout Page
    If you want to increase your AOV (Average Order Value), you can definitely start from the WooCommerce Checkout page. A client asked me to place a “Donation Area” close to the “Place Order” button (so at the bottom of the page, once customers are ready to pay) to drive more awareness around this add-on. All […]
  • 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: Exclude Hidden Products from Mini-Cart Counter
    When you add a hidden product to Cart, either manually or programmatically, this will be displayed in the Cart, Checkout and Order details pages (I’m not sure why a hidden product behaves like that… but thankfully you can hide hidden products from the Cart/Checkout/Order page with this snippet). Problem is, even if you hide hidden […]

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

28 thoughts on “WooCommerce: Hide Products Based on IP Address / Geolocation

  1. Hi Rodolfo,
    Thanks always for your codes.
    I have a question, in the two codes you exclude a country.
    In my case, however, I would like the products/category to be shown only in one country. So it would be more useful to just indicate the country in which it should be shown. It’s possible to do it?
    Thank you

    1. Of course! Instead of “=== ‘IT'”, use “!== ‘IT'” so you hide it everywhere except with that country

      1. Thanks Rodolfo!

        1. Hi Rodolfo,
          I placed the second code in my fuctions page of my child theme, indicated the country as you indicated and entered the product ID. Subsequently I did a test with one of the links you point out in your article “WooCommerce: How to Test Geolocation?” but it seems that the product is shown anyway.

          ...
          $hide_products = array( 1234 );  
              $country = $location['country'];   
              if ( $country !== "IT" ) {
          ...
          

          Am I doing something wrong? or does the code need to be updated?
          Thank you

          1. Have you enabled MaxMind from the Woo settings? Also, you meant to hide them for Italy?

  2. I need restrict some categories in state wise using geolocation

    1. As long as I know, MaxMind only gives you the country, and not the state I’m afraid

  3. Thanks for sharing this tutorial
    I want to limit it to a specific country, but Google’s robots are an exception.
    Please Help
    very important

    1. i need Exclude Bots (google and more) for seo

      1. Hi Ramos, 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!

  4. I need a code to hide/show products according to category to some countries.

    1. Hello Mario, 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. Thank you so much for the snippets and posts. I’m new to WooCommerce development and your site has taught and helped me do so many things with WooCommerce. I’ve struggled to use the official WooCommerce documentation as there are no examples for the actions and filters. Your examples have been very useful.

  6. Hi,
    In addition to this, is there a way to hide products based as below

    hide shipping class id (Heavy) for all customers over 50 miles from store?

    We need to stop people being able to see items that are not local to the store as they can’t be delivered. I’m looking for a plugin but can’t find anything and would rather code it in

    Thanks

    1. Hi John, 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!

  7. I am looking at YOUR SECOND SNIPPET AND IT’s close to what I am looking for but I want to show certain products per city. Let’s say someone logging in from this city sees only the products or shop from that city. What do I add to the second snippet to accomplish this? Thaks alot.

    1. Hi Nastin, 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!

  8. Rodolfo, are you thinking, would this snippet work to add similar products, located in nearby regions?
    I am looking for a solution to aggregate products only from certain regions or neighborhoods …

    1. No, only for countries. Otherwise you need a custom list of IP addresses, in which case, yes

  9. where i can find the countries code list ?

    1. Hi Hassan, inside the WooCommerce plugin, file: woocommerce\i18n\countries.php

  10. Hi,
    We want to implement hook for filtering product on home page based on customer location. The products on shop page and product page is getting filtered based on customer location. We want to implement hooks with filter or any other alternative way to filter product based on customer base location.
    We are presently using WCLover plugin for filtering on Shop page. But it does not support on home page.

    1. And you tried this method already?

  11. The snippet has your URL. How we do use it that way showing your URL. Regards Dennison.

    1. Sorry, can you explain that again please?

  12. Very nice!

    Is it possible to use this code and to hide whole category for a specific country?

    1. Hi Daniil, 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!

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 *