skip to Main Content

I would like to hide prices for products in specific categories, prices should be hidden only on shop and category page, but visible on product page, chart and subtotals.

Thank you!

This code has this functionality but I would like to apply it only on specific categories:

add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}

3

Answers


  1. You can get the cat name if you have the prodict id with the following:

     $term = get_term_by( 'id', $cat_id, 'product_cat' );
    
     echo $term->name;
    

    So simply write a condition to the cat name

        function remove_woocommerce_loop_price() {
            if($term->name == 'your_category')
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
    
    Login or Signup to reply.
  2. You can check if the product has a category by has_term function, and it’s an action hook not a filter

    add_action( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
    function remove_woocommerce_loop_price() {
        $category_name = 'the_category_name_here';
        if ( has_term( $category_name, 'product_cat' ) ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
    }
    
    Login or Signup to reply.
  3. To hide the price based on specific product category slugs, you can use this:

    // hide the price based on product categories
    add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
    function hide_price_for_specific_product_categories() {
    
        // set the slugs of the product categories for which prices must be hidden
        $categories = array(
            'clothing',
            'bags',
            'shoes'
        );
    
        global $product;
        // if the product belongs to at least one product category present in the "$categories" array, it hides the price
        if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
    
    }
    

    To hide the price based on specific product category ids, you can use this:

    // hide the price based on product categories
    add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
    function hide_price_for_specific_product_categories() {
    
        // set the ids of the product categories for which prices must be hidden
        $categories = array( 61 );
    
        global $product;
        // if the product belongs to at least one product category present in the "$categories" array, it hides the price
        if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
    
    }
    

    The code has been tested and works. Add it to your theme’s functions.php.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search