• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Woocommerce - how to hide price for certain products / category of products ?

kikirikimiki

Active member
Jul 1, 2020
210
227
43
Hello everyone,

I am trying some filtering with code injected with the snippets plugin - code looks something like this:

add_filter( 'woocommerce_get_price_html', 'hide_price_for_category', 10, 2 );
function hide_price_for_category( $price_html, $product ) {
// List of category slugs for which I want to hide the price to
$category_slugs = array( 'power-management' );

// Check if the product is in one of the specified categories
if ( has_term( $category_slugs, 'product_cat', $product->get_id() ) ) {
$price_html = '';
}

return $price_html;
}

where my target category's slug is 'power-management' - to no avail. Is there a plugin that does a better job ? Thank you!
 
Hello everyone,

I am trying some filtering with code injected with the snippets plugin - code looks something like this:

add_filter( 'woocommerce_get_price_html', 'hide_price_for_category', 10, 2 );
function hide_price_for_category( $price_html, $product ) {
// List of category slugs for which I want to hide the price to
$category_slugs = array( 'power-management' );

// Check if the product is in one of the specified categories
if ( has_term( $category_slugs, 'product_cat', $product->get_id() ) ) {
$price_html = '';
}

return $price_html;
}

where my target category's slug is 'power-management' - to no avail. Is there a plugin that does a better job ? Thank you!
Did you try this one?

PHP:
function hide_prices_specific_categories( $price, $product ) {
  if ( is_product_category('power-management') ) {
    return ''; 
  }
   
  return $price;
}

add_filter( 'woocommerce_get_price_html', 'hide_prices_specific_categories', 10, 2 );
 
Did you try this one?

PHP:
function hide_prices_specific_categories( $price, $product ) {
  if ( is_product_category('power-management') ) {
    return '';
  }
  
  return $price;
}

add_filter( 'woocommerce_get_price_html', 'hide_prices_specific_categories', 10, 2 );
It does the same .. nothing. I need to try editing the function.php instead of relying on snippets injection. BRB
 
Update - IT does hide them prices in the category page. I'm working on hiding the prices for each individual product - BRB with the solution - if it works.
 
Update - IT does hide them prices in the category page. I'm working on hiding the prices for each individual product - BRB with the solution - if it works.
Try this one:

PHP:
add_action( 'woocommerce_product_query', 'hide_products_category_specific' );
  
function hide_products_category_specific( $q ) {
 
    $tax_query = (array) $q->get( 'tax_query' );
 
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'power-management' ),
           'operator' => 'NOT IN'
    );
 
 
    $q->set( 'tax_query', $tax_query );
 
}
 
:) It hides the products in that category - No products were found matching your selection. - But the price of the individual product in that category still pops up.
 
:) It hides the products in that category - No products were found matching your selection. - But the price of the individual product in that category still pops up.
Sorry yes , try this one:

PHP:
add_filter( 'woocommerce_get_price_html','hide_price_product_specific');
function hide_price_product_specific( $price) {
global $product;
$hide_for_categories = array( 'power-management' );
if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
$price= '';
}
return $price;
}

Tested and approved on xstore theme
 
Works! Thanks a bunch man! There are some tweaks like removing the add-ons that are tallying the total - showing the initial price without them addons - but this is totally working.
Thanks again!
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock