Search code examples
wordpresswoocommercecode-snippets

Woocommerce Hide Price by Categories + is_product_category(), is_shop()


can you help me with the code below? I need to hide price for specific category but on shop and category pages, not on product detail or admin.

I need to add is_product_category() and is_shop() code but have no idea where exactly.

The Snippet is by jeroensormani.com

add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
    if ( is_admin() ) return $price;

    // Hide for these category slugs / IDs
    $hide_for_categories = array( 'singles', 'albums' );

    // Don't show price when its in one of the categories
    if ( has_term( $hide_for_categories, 'product_cat', $product->get_id() ) ) {
        return '';
    }

    return $price; // Return original price
}, 10, 2 );

add_filter( 'woocommerce_cart_item_price', '__return_false' );
add_filter( 'woocommerce_cart_item_subtotal', '__return_false' );


Solution

  • You can hide the price for specific categories on the shop page and category archive page using this code:

    add_filter('woocommerce_get_price_html', 'hide_price_on_shop_and_tax', 10, 2);
    
    function hide_price_on_shop_and_tax($price, $product){
        $hide_for_categories = array('singles', 'albums');
        if ((is_shop() || is_product_category()) && has_term($hide_for_categories, 'product_cat', $product->get_id())) {
            return false;
        } else {
            return $price;
        }
    }