Hide Out of Stock Products in Related Products for WooCommerce

Previously I wrote a post about hiding out of stock products in your WooCommerce store. One reader asked how they might go about hiding out of stock items only when viewing related products on the single product page. Well get ready, because here it is.

You can use a plugin like Code Snippets to add the following code to your website.

We’re basically toggling the “Hide out of stock items from the catalog” option found in WooCommerce > Settings > Products > Inventory using a filter. This means the option is never actually updated. Before the related products template is loaded we enable the option, and once it’s loaded and displayed, we disable the option.

You could also filter the related products query directly, using the same taxonomy query as explained in the original post, but this method seemed more reliable to me.

Leave a Reply

10 Comments on "Hide Out of Stock Products in Related Products for WooCommerce"

avatar
  Subscribe  
newest oldest most voted
Notify of
makeonlineshop
Guest

Thank you, I will try it and I have posted your page it here, hope it can help someone:

https://wordpress.org/suppo

jrotem
Guest

Thank you! The solution on github kept throwing an error, but your code worked.

Carret
Guest

The problem is that not showing the ones that are out of stock does not always show all possible related products. Sometimes it shows 3, another 2, another 4, or 5, 1 … even if there are enough stock-related products.

It seems that when looking for the configured number of products, if there are some without stock, discard them but not replace them with others.

Should be a woocommerce problem, as I think the same thing happens if you mark the option in settings to not show products out of stock.

kirpi
Guest

Right the same happens to me.

Elizabeth Drew
Guest

Thanks for this! Works exactly as promised from what I can see. I actually came across it as I am looking for a way to only show instock products on a certain page that uses a woocommerce shortcode. Pointing in the right direction for this would be much appreciated! Thanks again!

Kenneth
Guest
Hi, Thanks for sharing.We do you insert this snippet? I am looking at related.php $related = $product->get_related( $posts_per_page ); if ( sizeof( $related ) === 0 ) return; $args = apply_filters( ‘woocommerce_related_products_args’, array( ‘post_type’ => ‘product’, ‘ignore_sticky_posts’ => 1, ‘no_found_rows’ => 1, ‘posts_per_page’ => $posts_per_page, ‘orderby’ => $orderby, ‘post__in’ => $related, ‘post__not_in’ => array( $product->id ),) );
Jon-Paul Smith
Guest

Hi there,
Will this also work with cross-sells?

For some reason out of stock products show up in cross-sells in the cart area?!
I guess I can go through every product each time something sells and update the cross-sells, but it would way more handy if I could add some code that’ll stop out of stock products from being displayed 🙂
Alternatively is there a way to automatically select cross-sell products by category do you know?

Thanks for your help!

JP