Hide Product ID Based on Geolocated Country @ WooCommerce Shop

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 );
    }
}

ليست هناك تعليقات:

إرسال تعليق

Free Shipping Threshold @ Cart Page