0

I'm trying to hide the products price and add to cart button from woocommerce pages on specific countries.

i've found Turn Woocommerce shop into catalog for geolocated countries? but it works only for one country

I couldn't find a solution to change this code to be able to target more than 1 country. I made an array with all the countries I needed

$countries = array("IT","DE","FR","AT","BE","BG","CY","HR","DK","EE","FI","EL","IE","LV","LT","LU","MT","NL","PL","PT","CZ","RO","SK","SI","ES","SE","HU");

If someone could help me it would be great

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • 1
    You could make use of [in_array](https://www.php.net/manual/en/function.in-array.php), so you get `if ( in_array ( get_geolocated_user_country(), $countries ) ) {..` – 7uc1f3r Sep 03 '20 at 11:32

2 Answers2

0

That functionality, as I’m sure you’ve noticed, isn’t part of the core features of the WooCommerce plugin.

Also, without knowing which plugins you’ve already considered, we might be covering the same ground twice. With that said, you’ll definitely want to look into using 3rd party plugins for that. Possible options include:

https://www.extendons.com/product/woocommerce-geolocation-based-products-plugin-geo-ip-filter/ https://codecanyon.net/item/woocommerce-geolocation-plugin-ip-based-products-filter/22022059 https://cyberchimps.com/how-to-redirect-based-on-geolocation-on-wordpress/

Note: You’ll want to contact each of the respective developers to make sure their products meet your project requirements.

psycho developer
  • 222
  • 2
  • 12
  • I haven't considered a plugin yet because i used the code i posted early on another website and it worked because i only needed to show prices for italian users, if i can't find a solution i think i'll consider one of the plugin you posted. Thank you! – Riccardo Basso Sep 03 '20 at 11:50
0

You could use this snippet below, piggybacking off of the built-in geolocation of WooCommerce. You will need to make sure geolocation is enabled in your WooCommerce general settings.

$userLocation = WC_Geolocation::geolocate_ip();
$userCountry = $userLocation['country'];
$countries = array("IT","DE","FR","AT","BE","BG","CY","HR","DK","EE","FI","EL","IE","LV","LT","LU","MT","NL","PL","PT","CZ","RO","SK","SI","ES","SE","HU");
    
if ( in_array($userCountry, $countries) ) { 
    // your code here to hide price & add to cart
}

Geolocation info here: https://woocommerce.github.io/code-reference/classes/WC-Geolocation.html

J Cox
  • 21
  • 2