Woocommerce - Hide Price in Specific Category

xlx

Active member
Dec 13, 2018
141
53
28
Hello,

I have a ecommerce cart, built on woocommerce.
I need to hide the price in the category page for specific categories.
But the price should be displayed in the individual product page

Ive used this piece of code

PHP:
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

following this article

The only issue being it hides the price for all categories, not specific ones
for example i want to hide headphones and covers.

Can someone please edit the code to hide only specific categories.

Thanks for helping
 
Last edited:

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,089
27,346
120
You should add a filter like:

Code:
add_filter( ‘woocommerce_get_price_html‘, function( $price, $product ) {
    $hide_for_categories = array( ‘category1‘, ‘category2‘ );
    if ( has_term( $hide_for_categories, ‘product_cat‘, $product->get_id() ) ) {
        return ‘‘; // hide price if in hidden price category list
    }
    return $price; // show price if not in hidden price category list
}, 10, 2 );

Add this code to your theme or child theme function.php
 
  • Like
Reactions: xlx

xlx

Active member
Dec 13, 2018
141
53
28
@slvrsteele Thanks for your awesome help.

This code does work on the shop page, and it does not show the price in the selected category.
But it also hides the price once i click the product, i do want to show the price when the user clicks on the individual product.

Thanks again for helping
 

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,089
27,346
120
Just change the condition:

Code:
add_filter( ‘woocommerce_get_price_html‘, function( $price, $product ) {
    $hide_for_categories = array( ‘category1‘, ‘category2‘ );
    if ( is_shop() && has_term( $hide_for_categories, ‘product_cat‘, $product->get_id() ) ) {
        return ‘‘; // hide price if in hidden price category list
    }
    return $price; // show price if not in hidden price category list
}, 10, 2 );

or a bit dirtier add a second if

Code:
add_filter( ‘woocommerce_get_price_html‘, function( $price, $product ) {
    $hide_for_categories = array( ‘category1‘, ‘category2‘ );
    if ( is_shop() ) {
        if ( has_term( $hide_for_categories, ‘product_cat‘, $product->get_id() ) ) {
            return ‘‘; // hide price if in hidden price category list
        }
        return $price; // show price if not in hidden price category list
    }
    return $price; // show price if not shop page
}, 10, 2 );
 
Last edited by a moderator:
  • Like
Reactions: xlx

xlx

Active member
Dec 13, 2018
141
53
28
Thanks @slvrsteele .. ill surely check this and get back to you.

Appreciate your time and patience.
Thanks a Ton!
 
  • Like
Reactions: Mr G

xlx

Active member
Dec 13, 2018
141
53
28
@slvrsteele This works fantastic. thanks for helping bro,

Now i realize i failed to share a vital piece of information, the above code works flawlessly on my shop page. I have made a separate page for sale and have used wpbakery to pull products from my special categories on this page.

Can the same functionality be added to this specific page?
Really appreciate for your patience.
 

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