WooCommerce Related Products To Show In Stock Products Only


There must be out-of-stock products showing under Related Products on your single product page.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

Related Products, which are shown on the single product template, are pulled from your store which share the same tags or categories as the currently viewed product.

For example, if you are looking at a pair of socks which are part of the clothing category, you will see under the Related Products heading products which also belong to this clothing category.

By default, Related Products are sorted randomly (as are manually defined Cross-Sells and Upsells).

Related Products, despite not being a manually defined cross-sell or upsell, act as an additional purchase, potentially increasing your Average Order Value (AOV).

So it makes sense only in-stock related products are shown and out-of-stock related products are hidden.

This code snippet targets only the single product page. It hides all out-of-stock related products and will only show in-stock related products.

Because the code snippet only targets the single product template, out-of-stock products will still be shown elsewhere they are generated like the shop and category pages.

To target other locations, you’ll need to change line 8 in the code.

* Snippet Name: WooCommerce Show Only In Stock Related Products
* Snippet Author: ecommercehints.com

add_action( 'pre_get_posts', 'ecommercehints_in_stock_related_products_only' );
function ecommercehints_in_stock_related_products_only( $query ){
if( is_product() && $query->is_main_query() ) { // Only on the product page where Related Products are shown
$query->set( 'meta_key', '_stock_status' );
$query->set( 'meta_value', 'instock' );

Snippet Benefits

  • Increase the Average Order Value (AOV) by showing only Related Products which are in stock.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

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

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers