How to Hide Out of Stock Products

By default, when a product is out of stock it is still going to be displayed on your shop and product archive pages and there is going to be “Read more” button instead of “Add to cart”, like this:

Out of stock items are displayed by default

But if you are reading this, I suppose you do not want them to be displayed, right? ;)

But wait, wait, there is also another option – we can display products in stock always first.

How to Show or Hide out of Stock Products in Settings

First things first – there is even no needs of using code snippets or any plugins in order to achieve that, you can just go to WooCommerce settings, Products tab > Inventory.

hide our of stock products in WooCommerce settings
Once the checkbox is checked, out of stock products won’t be displayed anywhere on the website.

The products will still be available by the direct URL, which is great, because products could be bookmarked in the browsers. But the products won’t be displayed on shop page, product category and product tag archive pages, search results, related products, upsells and cross-sell sections.

Hide Out of Stock Products Programmatically

Products has a meta key _stock_status which can be instock or outofstock (and on backorder).

I saw an example on the internet, where a guy suggested to pass meta_query parameter to the woocommerce_output_related_products_args filter hook. I want to clarify one moment – this hook is not about passing args into WP_Query. The filter hook supports only posts_per_page, columns, orderby and order parameters. That’s all!

So if you really need to hide your out of stock products from some specific places on the website and somehow the first method is not what you need, I recommend you to check pre_option_{option name} filter hook.

/**
 * Hide out of Stock Products in WooCommerce 
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#programmatically
 */
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock' );

function misha_hide_out_of_stock( $option ) {

	if( is_admin() ) { // do nothing in WordPress admin
		return $option;
	}
	
	$option = 'yes';
	return $option;
}

As you can see in the above code, we excluded /wp-admin/, but you can also exclude any other pages using conditional tags. For example, let’s allow out of stock products to be displayed in search.

if( is_search() ) {
	return $option;
}

And of course you can also do that with meta_query and pre_get_posts action hook, but please be careful, because this code may also be applied to your blog posts or anything else on the website.

/**
 * Hide out of Stock Products in WooCommerce 
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#programmatically
 */
add_action( 'pre_get_posts', 'misha_hide_out_of_stock_in_search' );

function misha_hide_out_of_stock_in_search( $query ){
	// run the code only on the Shop page 
	if( is_shop() && $query->is_main_query() ) {
		$query->set( 'meta_key', '_stock_status' );
		$query->set( 'meta_value', 'instock' );
	}
}

This method may seem a little bit tricky, but it works perfectly.

/**
 * Related Products – Hide out of Stock
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#related-products
 */
function misha_hide_out_of_stock_option( $option ){
	return 'yes';
}

add_action( 'woocommerce_before_template_part', function( $template_name ) {
	if( 'single-product/related.php' !== $template_name ) {
		return;
	}
	add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock_option' );
} );

add_action( 'woocommerce_after_template_part', function( $template_name ) {
	if( 'single-product/related.php' !== $template_name ) {
		return;
	}
	remove_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock_option' );
} );
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter