Hide the price for specific WooCommerce products

Do you want to hide the price for a certain product or multiple products in your online WooCommerce store? Doing so is actually quite easy. In this snippet we’ll show you how.

To get this working, what we need to do is add a filter that intercepts the rendering of the price for the product, check if the product we’re currently rendering to the page is one that we don’t want to display the price of, and if it is, return an empty price. Otherwise, we output the price as usual.

Snippet to hide the price for a specific WooCommerce product

<?php
/**
 * Hide the price of a product or multiple products.
 *
 * @param string $price The existing price.
 * @param object $product The product object.
 * @return string
 */
function thenga_hide_price_for_product( $price, $product ) {
   // Populate this array with the ID's of product you want to hide the price of.
   $products_to_hide = array( 1, 2, 3 );

   if ( in_array( $product->get_id(), $products_to_hide, true ) ) {
      return '';
   }

   return $price;
}
add_filter( 'woocommerce_get_price_html', 'thenga_hide_price_for_product' );

// Apply these filters to hide the price from the cart and checkout pages.
add_filter( 'woocommerce_cart_item_price', '__return_false' );
add_filter( 'woocommerce_cart_item_subtotal', '__return_false' );

You’ll notice the last two filters that we’ve added to the bottom of the snippet. These should be used if you want to hide the price from the cart and checkout page. Unfortunately, it doesn’t remove the entire line item but with a CSS rule or two, you can adjust the layout to suit your needs.

You may also be interested in changing the price display if a product is free. We hope you found this snippet useful, hit us up in the comments if you struggle with anything and we’ll try our best to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.