• Resolved porseshgar

    (@porseshgar)


    I would like to hide Out of Stock products from Related products in single product pages. Is it possible?
    I found this snippet but it didn’t work out!

    add_filter( 'woocommerce_product_related_posts_query', 'alter_product_related_posts_query', 10, 3 );
    function alter_product_related_posts_query( $query, $product_id, $args ){
        global $wpdb;
    
        $query['join']  .= " INNER JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id ";
        $query['where'] .= " AND pm.meta_key = '_stock_status' AND meta_value = 'instock' ";
    
        return $query;
    }
    
    

    Any track is appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi,

    Thanks for contacting us about hiding out of stock products from the Related Products section of the single product page. I’d recommend reading this article as it has helped others with the same issue in the past:

    https://iconicwp.com/blog/hide-stock-products-related-products-woocommerce/

    Please let us know if this works for you. Have a great day.

    Thread Starter porseshgar

    (@porseshgar)

    Thank you very much indeed @phillipwoo ; I already saw that page and I’ve tested it before coming to the Woocommerce support page!
    This code doesn’t work for me!
    Would you show me another solution please?

    Also interested if you can find…

    Are you wanting to hide these products from the Catalog as well? If so then I recommend using the core feature found here:

    WooCommerce → Settings → Products Tab → Inventory

    Then check the “Out of stock visibility” checkbox to hide out of stock items.

    Testing is also showing that the code sample provided in the link in my last response works very well. You would need to add it to your site using a plugin like Code Snippets (https://www.ads-software.com/plugins/code-snippets/) so that any updates to WordPress core doesn’t overwrite them.

    If after trying this nothing works I recommend you perform a complete conflict test on your site to see where the issue is coming from. You can learn more about this kind of testing here:

    https://docs.woocommerce.com/document/how-to-test-for-conflicts/

    Please keep us updated on your progress, as things evolve with your site we should be able to get a better idea of where and why things are not working the way you would like. This will enable us to find the right fix for your needs.

    Take care!

    Here you go, add to functions.php – this will hide out of stock products from related products.

    add_filter( 'woocommerce_output_related_products_args', function( $args )
    {
        $args = wp_parse_args( array(
            'posts_per_page' => 4,
            'meta_query' => array (
               'key' => '_stock_status',
               'value' => 'instock'
        )
        ), $args );
        return $args;
    });

    hello, any idea how to hide from RELATED products that belong to a specific category ?
    Because i do not declare as OUT OF STOCK products that are temporary not available (because it’s only temporary), I only add them to a special category. Still, I would prefer that visitors do not see them in RELATED.
    Thank you for your help to all.

    Not tested @catboulin but this might work:

    add_filter( 'woocommerce_output_related_products_args', function( $args )
    {
        $args = wp_parse_args( array(
            'posts_per_page' => 4,
            'tax_query' => array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 11, 12, 13 ),
                'operator' => 'NOT IN',
        ),
        ), $args );
        return $args;
    });

    change the IDs in the array for the ones you want to exclude

    • This reply was modified 5 years, 7 months ago by James Hunt.

    @catboulin the answer @bonkerz posted above should work. Alternatively, if you’d like to invest more heavily in rule-based product recommendations like this, have a look at WooCommerce Product Recommendations.

    The extension allows you to use filters to include/exclude products depending on their category/tag, stock status, price and creation date, to name a few.

    Cheers,
    Manos
    SomewhereWarm

    @catboulin the answer @bonkerz posted above will never work because the filter hook woocommerce_output_related_products_args doesn’t accept the meta_query parameter.

    hello, thank you but I am looking for a code solution, not for a plugin to add. Any better idea ? thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Hide out of stock from related products’ is closed to new replies.