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!
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 );
}
}
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
Of course! Instead of “=== ‘IT'”, use “!== ‘IT'” so you hide it everywhere except with that country
Thanks Rodolfo!
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.
Am I doing something wrong? or does the code need to be updated?
Thank you
Have you enabled MaxMind from the Woo settings? Also, you meant to hide them for Italy?
I need restrict some categories in state wise using geolocation
As long as I know, MaxMind only gives you the country, and not the state I’m afraid
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
i need Exclude Bots (google and more) for seo
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!
I need a code to hide/show products according to category to some countries.
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!
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.
Cheers
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
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!
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.
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!
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 …
No, only for countries. Otherwise you need a custom list of IP addresses, in which case, yes
where i can find the countries code list ?
Hi Hassan, inside the WooCommerce plugin, file: woocommerce\i18n\countries.php
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.
And you tried this method already?
The snippet has your URL. How we do use it that way showing your URL. Regards Dennison.
Sorry, can you explain that again please?
Very nice!
Is it possible to use this code and to hide whole category for a specific country?
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!