Sometimes you may want to hide product prices and the Add to Cart button for certain product categories — for example, when displaying items that are for inquiry only, not yet for sale, recently sold, or available on request.
WooCommerce doesn’t include this feature by default, but we can easily add it with a few lines of PHP.
Hiding Price and Add to Cart Button for a Specific Category
The code below does exactly that. It hides the price and Add to Cart button for products in a chosen category — both in the shop loop and on the single product page.
/**
* Hide product price and Add to Cart button for a specific category
*/
add_action( 'wp', function() {
$category_name = 'the_category_name_here'; // Replace with your category slug.
// Hide in shop loop and category archives.
if ( is_shop() || is_product_category() || is_product_taxonomy() ) {
if ( has_term( $category_name, 'product_cat' ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
}
// Hide on single product page.
if ( is_product() ) {
global $post;
if ( has_term( $category_name, 'product_cat', $post->ID ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
});
How It Works
- The function runs on the
wp
action, which fires after WordPress has fully loaded the current post or product context. - It checks if the current product belongs to a specific category using
has_term()
. - It removes the WooCommerce hooks that output the price and Add to Cart button:
woocommerce_template_loop_price
woocommerce_template_loop_add_to_cart
woocommerce_template_single_price
woocommerce_template_single_add_to_cart
- You can easily expand the condition to support multiple categories:
if ( has_term( array( 'category-one', 'category-two' ), 'product_cat', $post->ID ) ) {
// same code here
}
Hiding by Category ID Instead of Slug
If you prefer to use category IDs instead of slugs, you can easily modify the logic. The code below demonstrates how to hide prices based on product category IDs.
/**
* Hide product price based on specific category IDs.
*/
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, 75 ); // Replace with your category IDs.
global $product;
// If the product belongs to any of these categories, hide the price.
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
This approach uses the same has_term()
function, but checks against category IDs instead of slugs.
You can list multiple category IDs in the $categories
array if needed.