How to Hide Products Without Price in WooCommerce

WooCommerce Hide Products Without PriceIf you want to hide products without the price, you can quickly implement this snippet in your functions.php or your plugin code, and all the products without a price will be hidden from the customer view. It’s not complicated just adding the code in the theme functions or the plugin files.

WooCommerce Hide Products Without Price

First, you need to create a meta query for all the products that will get the products and in the meta query you will look for the empty value of the key – price and if the value is empty you can reset the meta query.

This meta query needs to hook on this action woocommerce_product_query  and the callback function will contain the logic to show all products apart from those without the price.

You can have the code as follows :

add_action( 'woocommerce_product_query', 'njengah_hide_products_without_price' );

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

This code should be added to the functions.php and you will successfully hide all the products without the price.  As you can see on the code callback function the value of the meta query is set to an empty string ” “, this is the basis of the WooCommerce hide products without price logic.

WordPress Meta Queries

If you have not worked with meta queries before, you need to learn from this useful WordPress development resource – WP_Meta_Query

Basically WP_Meta_Query is a class that is defined in wp-includes/meta.php that generates the necessary SQL for meta-related queries.

Accepted Arguments

The following arguments can be passed in a key=>value paired array.

  • meta_key (string) – Custom field key. ( You must sanitize this yourself )
  • meta_value (string|array) – Custom field value. ( You must sanitize this yourself )
  • meta_type (string) – Custom field type (see type below for options).
  • meta_compare (string) – Operator to test the 'meta_value' (see compare below for possible values).
  • meta_query (array) – Contains one or more arrays with the following keys:
    • key (string) – Custom field key.
    • value (string|array) – Custom field value. It can be an array only when compare is 'IN''NOT IN''BETWEEN', or 'NOT BETWEEN'. You don’t have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.
      (Note: Due to bug #23268value was required for NOT EXISTS comparisons to work correctly prior to 3.9. You had to supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)
    • compare (string) – Operator to test. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’.
    • type (string) – Custom field type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.

Conclusion

In this post, we have outlined the quick way you can employ on your WooCommerce site to hide those products without the price from the shop page by just resetting the meta query to eliminate those products whose price value is empty.

Similar Articles

Comments are closed.