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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its 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 guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went 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.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 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.

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

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

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

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

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

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

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

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

  8. where i can find the countries code list ?

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

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

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

  11. 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? 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. Required fields are marked *