WooCommerce Hide Products Without Price (Simple Fix) - React to WordPress

WooCommerce Hide Products Without Price (Simple Fix)

Hello and welcome to this simple snippet, Here I will show you on how to hide WooCommerce products which don’t have a price or the price is zero.

Also see WooCommerce Hide Product Price (No Plugin Needed)

How to Apply “WooCommerce Hide Products Without Price”?

This is relatively simple; you have two options, choose one:

  1. Edit your functions.php file with your favorite FTP client add this functions to the end of the file.
  2. Edit through the WordPress theme editor located at Appearance > Editor > functions.php and add this functions to the end of the file.

Hide Products Without Price Hook

1. Hide products with the price set to zero in WooCommerce

add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_zero' );
function react2wp_hide_products_higher_than_zero( $q ){
   $meta_query = $q->get( 'meta_query' );
   $meta_query[] = array(
      'key'       => '_price',
      'value'     => 0,
      'compare'   => '>'
   );
   $q->set( 'meta_query', $meta_query );
}

If you add this function to your theme `s functions.php it will make the wp_query to show only products which have the price higher than zero.

2. Hide products without price in WooCommerce

add_action( 'woocommerce_product_query', 'react2wp_hide_products_without_price' );
function react2wp_hide_products_without_price( $q ){
   $meta_query = $q->get( 'meta_query' );
   $meta_query[] = array(
      'key'       => '_price',
      'value'     => '',
      'compare'   => '!='
   );
   $q->set( 'meta_query', $meta_query );
}

And this hook is allowing you to show all the products besides those that are without ANY price(products with a zero price tag – will be still showing).

That’s it for this snippet. Thank you for your time and hopefully, it helped you!

More about: Best WordPress web hosting in 2018  

2 comments on “WooCommerce Hide Products Without Price (Simple Fix)”

  1. Very nice! Shouldn’t the first one be named something like “react2wp_show_products_higher_than_zero” though?

Leave a Reply

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