Bookmark

No account yet? Register

Difficulty: Easy

Here we will cover how to hide products that have no price assigned on your WooCommerce store. Hiding the products that have no price assigned is very simple and can be done like many other things on your WooCommerce store by adding code to your Functions.php file in your theme folder.

TLDR
File: Functions.php

add_filter( 'woocommerce_product_query_meta_query', 'shop_only_products_with_price', 10, 2 );
function shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     $meta_query[] = array(
     'key' => '_price',
     'value' => '',
     'type' => 'numeric',
     'compare' => '!='
     );
     return $meta_query;
}

Hiding products with no price set

Customizing functions.php file in WordPress

To hide products that have no price set first navigate to your admin interface and then go to Appearance and then under Appearance select Theme Editor.

After navigating there you will see a windows like this one below. In there select Theme Functions (functions.php) and then at the end put this code.

add_filter( 'woocommerce_product_query_meta_query', 'shop_only_products_with_price', 10, 2 );
function shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     $meta_query[] = array(
     'key' => '_price',
     'value' => '',
     'type' => 'numeric',
     'compare' => '!='
     );
     return $meta_query;
}

Click on button bellow to update your file/save changes and that’s it. Now your WooCommerce products with no price set will be hidden from your WebShop.

Editing the code

There are a few possibilities on how you can edit this code to fit your purpose. Maybe you also want to hide products that have price 0 or any other value. Here is an example on how to hide products that have price set to zero.

Hiding products with price 0

To hide products that have price set to 0, we just need to change one thing in the above code. In meta query array we change ‘value’ => ” to ‘value’ => 0 which results in the following code:

add_filter( 'woocommerce_product_query_meta_query', 'shop_only_products_with_price', 10, 2 );
function shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     $meta_query[] = array(
     'key' => '_price',
     'value' => 0,
     'type' => 'numeric',
     'compare' => '!='
     );
     return $meta_query;
}