Hide Out of Stock Products in Related Products for WooCommerce

14 Shares

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.

14 Shares

17 Comments

  1. makeonlineshop says:

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

    https://wordpress.org/suppo

  2. jrotem says:

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

  3. Carret says:

    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.

  4. Elizabeth Drew says:

    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!

  5. Kenneth says:

    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 ),
    ) );

  6. 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

  7. Domin says:

    Hello,
    The code does not work for me.
    Should the path “single-product/related.php” look different if a theme have the file in woocommerce/single-product/related.php? Is that change anything?
    Regards,
    Domin

  8. Sarah says:

    Hi.
    It’s not compatible with Woocommerce v3.5.5 (Latest update) , and it doesn’t work at all; Would you update it please?
    Thanks

  9. MakeOnlineShops says:

    hello,

    Possible to hide products from a specified category from Related products ?

    I have a category of products that are temporary not available and i want to hide these products from the related products block.

    Thank you.

  10. James says:

    I’m sorry but this doesn’t work properly. It hides the out of stock product, so related products will sometimes show 1,2,3 products instead of 4 (or whatever setting a user has).

  11. James says:

    I’m sorry but this doesn’t work properly. It hides the out of stock product, so related products will sometimes show 1,2,3 products instead of 4 (or whatever setting a user has).

  12. […] have followed this blog post, which a lot of other sites have as the solution to this problem, but it does not work properly (it […]

Leave a Reply

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