How to Hide Price If Zero In WooCommerce

WooCommerce Hide Price if ZeroWooCommerce was built with flexibility in mind. This means that you can customize your store to fit your brand requirements. When adding products to your WooCommerce store, you are required to add the price so that customers can see them.

When the product is not available or out of stock, then many store owners make the price zero. This gives an expression to the user that the product is not available. However, it would be easier to hide the product so that users can’t see them.

WooCommerce Hide Price If Zero

If your theme is built the right way, and working correctly with WooCommerce, the filter I will share should remove any product that is priced at $0 from the front end and still leave you the option to set up the price in the backend.

Additionally, there are some cases where you might be offering a free product, and I will provide a solution for you if you do not want to hide the product. Instead, you can display “FREE” at the single product page.

Steps to Hide Products if Price is Zero

This is how the product is displayed on the shop page:product priced at $0

Here are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to hide the product if the price is zero.
  3. Add the following code to the functions.php file:
add_action( 'woocommerce_product_query', 'njengah_product_query' );

function njengah_product_query( $q ){

    $meta_query = $q->get( 'meta_query' );

        $meta_query[] = array(

                    'key'       => '_price',

                    'value'     => 0,

                    'compare'   => '>'

                );

    $q->set( 'meta_query', $meta_query );

}
  1. Click Update to save the file.
  2. Open your shop page you will see the product with price zero is not shown on the products page.hide product if price is zero

Hiding Price if Zero

However, as I had mentioned earlier, there are some cases you might want to display “FREE” Instead of $0.00 or an empty price. Therefore, in this case, you do not want to hide the product. This means that we have to hide the price first and then display the “FREE”. Moreover, “FREE” looks much better than “$0.00”.product price is zero

Steps to Hide Price if Zero and Display “Free”

Here are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to hide the price if zero and display “Free”.
  3. Add the following code to the functions.php file:
/**

*       Display FREE if Price Zero or Empty - WooCommerce Single Product

*/

add_filter( 'woocommerce_get_price_html', 'njengah_price_free_zero_empty', 9999, 2 );

function njengah_price_free_zero_empty( $price, $product ){

if ( '' === $product->get_price() || 0 == $product->get_price() ) {

$price = '<span class="woocommerce-Price-amount amount">FREE</span>';

}

return $price;

}
  1. Click Update to save the file.
  2. This will be the outcome:hide price if it is zero

Conclusion

In this brief tutorial, you have seen how it is very easy to hide products if they are priced at $0. Moreover, you have learnt how to hide the price if it is $0 and replace it with “Free” or any other word you might prefer. Remember to add the code snippets at the bottom of your child theme’s functions.php file. If you encounter any problems, please contact a qualified WordPress developer.

Similar Articles

 


Customer Reviews

NJENGAH

Customer Reviews

Jeffrey Todd 04/07/2020

Exceptional Plugin & Service

I'm in the process of building my new website, and I had a question about changing some text within the WooCommerce plugin. I was spinning my wheels trying to figure it out. Joe reached back and worked with me - going above and beyond! - until I fully understood how to effectively change what I wanted. How can you ask for more than that? I highly recommend WooCommerce (and Joe!), the plugin and service are exceptional!
-Jeffrey Todd (MyLifeChart.com)