Bookmark

No account yet? Register

Difficulty: Easy

In this guide we will cover how to hide products without price set in your Woocommerce store. Hiding the products without price prevents those products from ever showing up to your customers which is very helpful and comes very handy when running a webshop. Like many other things in WordPress, hiding products without price can be achived by adding code to your Functions.php file in your theme folder.

TLDR SECTION

//hooking into filter
add_filter( 'woocommerce_product_query_meta_query', 'woowiki_shop_only_products_with_price', 10, 2 );

function woowiki_shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     //This meta query hides all prroducts with no price set or price is equal to zero
     $meta_query[] = array(
		array(
			'key' => '_price',
			'value' => 0,
			'type' => 'numeric',
			'compare' => '>'
		),
		array(
			'key' => '_price',
			'value' => '',
			'type' => 'numeric',
			'compare' => '!='
		),
		'relation' => 'AND',
     );
     return $meta_query;
}

Hiding products without price

Customizing functions.php file in WordPress

To hide products without price you will first need to navigate to your admin interface and then go to Appearance and then under Appearance select Theme Editor.

After navigating there you will see a window like this one below. In there On the right side select Theme Functions (functions.php)

At the end of your functions.php add (copy-paste) the code provided below.

//hooking into filter
add_filter( 'woocommerce_product_query_meta_query', 'woowiki_shop_only_products_with_price', 10, 2 );

function woowiki_shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     //This meta query hides all prroducts with no price set or price is equal to zero
     $meta_query[] = array(
		array(
			'key' => '_price',
			'value' => 0,
			'type' => 'numeric',
			'compare' => '>'
		),
		array(
			'key' => '_price',
			'value' => '',
			'type' => 'numeric',
			'compare' => '!='
		),
		'relation' => 'AND',
     );
     return $meta_query;
}

After you finished implementing the above code click on button bellow to update your file/save changes and that’s it.

Image shows implemented code for hide products with price set to zero and products without price

Now your Woocommerce products without price or products that have zero as a price will be hidden from your web shop.

Editing the code

There are a few possibilities on how you can edit this code to fit your purpose. Maybe you want just to hide products that have no price set or maybe you want to hide products that have price set to 0. Below are guides on how you can accomplish that.

Hiding products with price set to zero

To only hide products that have price set to 0, we just need to change few lines in the above code. We need to remove from $meta_query[] array for querying products without any price set and change relation. After doing that we end up with this code that will only hide products that have price set to zero.

//hooking into filter
add_filter( 'woocommerce_product_query_meta_query', 'woowiki_shop_only_products_with_price', 10, 2 );

function woowiki_shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     //This meta query hides all prroducts with no price set or price is equal to zero
     $meta_query[] = array(
		array(
			'key' => '_price',
			'value' => 0,
			'type' => 'numeric',
			'compare' => '>'
		)
    );
     return $meta_query;
}

Hiding products without price

To only hide products without price, we just need to change few lines in the above code as we did before. We need to remove from $meta_query[] array for querying products that have price set to more than zero set and change relation. After doing that we end up with this code that will hide products that only hide products without price.

//hooking into filter
add_filter( 'woocommerce_product_query_meta_query', 'woowiki_shop_only_products_with_price', 10, 2 );

function woowiki_shop_only_products_with_price( $meta_query, $query ) {
     if( is_admin() ) return $meta_query;
     //This meta query hides all prroducts with no price set or price is equal to zero
     $meta_query[] = array(
		 array(
			'key' => '_price',
			'value' => '',
			'type' => 'numeric',
			'compare' => '!='
		 )
     );
     return $meta_query;
}

Write A Comment