phpwordpresswoocommercecategories

Hide Price based on product category in Woocommerce


In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not

add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );

function woocommerce_remove_prices( $price, $product ) {
     if(is_product_category('sold')){   
        $price = '';
        return $price;
     } 
}

Solution

  • To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:

    add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
    add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
    function woocommerce_remove_prices( $price, $product ) {
        if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
            $price = '';
    
        return $price;
    }
    

    It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.

    Code goes in function.php file of your active child theme (or active theme).


    Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:

    // Specific product category archive pages
    add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
    function hide_loop_product_prices(){
        global $product;
    
        if( is_product_category('sold') ):
    
        // Hide prices
        remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        // Hide add-to-cart button
        remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );
    
        endif;
    }
    
    // Single product pages
    add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
    function hide_single_product_prices(){
        global $product;
    
        if( has_term( 'sold', 'product_cat', $product->get_id() ) ):
    
        // Hide prices
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    
        // Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
        if( ! $product->is_type('variable') ){
            remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
        } else {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        }
    
        endif;
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    Tested and works.