skip to Main Content

currently I hide all product price for out of GB but I would like to show product price only if they have a TAG.

my code to hide product price out of GB is anyone can help me ? please ?

/** hiding prices if outside of gb*/
add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
    // Get an instance of the WC_Geolocation object class
    $geo_instance  = new WC_Geolocation();
    // Get geolocated user geo data.
    $user_geodata = $geo_instance->geolocate_ip();
    // Get current user GeoIP Country
    $country = $user_geodata['country'];

    return $country !== 'GB' ? '' : $price;
}
/** fin hiding prices if outside of gb*/

thank you

show product price for out of GB and show product price only if they have a TAG

2

Answers


  1. Chosen as BEST ANSWER

    Thank you Faisal ! you help me so much :)

    first impression, I thought it working but prices are hided on GB as well is product haven't tag.

    GB = can see everything outside of GB = can see prices only product has the tag


  2. You can modify your code this way:

    // This will show product prices for products with a specific tag and outside of GB
    
    add_filter( 'woocommerce_get_price_html', 'show_price_based_on_country_and_tag', 10, 2 );
    function show_price_based_on_country_and_tag( $price, $product ) {
      
      // Get an instance of the WC_Geolocation object class
        $geo_instance  = new WC_Geolocation();
       
     // Get geolocated user geo data.
        $user_geodata = $geo_instance->geolocate_ip();
     
       // Get current user GeoIP Country
        $country = $user_geodata['country'];
        
      
      // Check if the product has the tag 'my_tag'
        $tag = 'my_tag';
        $has_tag = has_term( $tag, 'product_tag', $product->get_id() );
    
        // This will show price only if the user is outside of GB and the product has the tag 'my_tag'
        return ( $country !== 'GB' && $has_tag ) ? $price : '';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search