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

kikirikimiki

Active member
Jul 1, 2020
165
153
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!
 

guguk

Well-known member
Jul 19, 2019
1,148
828
113
Ottoman Empire
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 );
 

kikirikimiki

Active member
Jul 1, 2020
165
153
43
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
 

kikirikimiki

Active member
Jul 1, 2020
165
153
43
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.
 

guguk

Well-known member
Jul 19, 2019
1,148
828
113
Ottoman Empire
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 );
 
}
 

kikirikimiki

Active member
Jul 1, 2020
165
153
43
:) 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.
 

guguk

Well-known member
Jul 19, 2019
1,148
828
113
Ottoman Empire
:) 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
 

kikirikimiki

Active member
Jul 1, 2020
165
153
43
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!
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu