Okay
  Public Ticket #2075701
Hide out of stock products from related section inside product page
Closed

Comments

  • dichagr started the conversation

    Hello,

    I want to hide out of stock items in the related products section on the product page, but I don't want to hide them comletely from the eshop. I want people to see them in shop or search, but not in the related products section inside the product page. 

    Could you please provide a solution for this?

    Thanks

  • [deleted] replied

    Hi dichagr,

    I think that this is not possible in WooCommerce plugin but please contact WooCommerce support any maybe they can head you to the right direction.

    Thanks!

  • dichagr replied

    Hi Ylli, I think that Arlind has some kind of a solution for this, because he advised me ten minutes ago to open a new ticket (we were talking in an old ticket) so that others can see the solution on this. 

    Anyway, I know this is possible in woocommerce, using the 'woocommerce_output_related_products_args' hook to filter them out but it's not working as it should. 

    It seems like aurum overrides this with its own filter which is possible because I see an option at

    laborator > theme options > shop settings > Related products count: 4

    that may use this particular hook or something similar and overrides mine.

    My filter generally works, but it doesn't on your theme, that's why I opened this ticket so you can help me out in some way. 

    Thanks again

  • [deleted] replied

    Hi dichagr,

    I'm sorry but this is a customization question and support doesn't offer customization services as we wrote on our support homepage and on this area.

    Would like to thank you for your understanding!

  • dichagr replied

    It's not a customization question actually. I'm not asking you how to make this customization. I using a universal woocommerce hook that should work, but it doesn't work on your theme. 

    That's why I ask you guys if there is a conflict. I can't search all your theme code to find it myself.

    Please don't post the same reply or let Arlind check this topic out because we were talking with him previously and he knows the case

  • [deleted] replied

    Hi dichagr,

    Ok then, I will forward this ticket to Arlind so he will continue the conversation that you started.

    Thanks for your understanding!

  • [deleted] replied

    Hi dichagr,

    I have created a code snippet for you to hide out of stock products:

    function hide_out_of_stock_items_from_wc_shortcode( $query_args ) {
        $product_visibility_term_ids = wc_get_product_visibility_term_ids();
        $query_args['tax_query'][] = array(
            array(
                'taxonomy' => 'product_visibility',
                'field'    => 'term_taxonomy_id',
                'terms'    => $product_visibility_term_ids['outofstock'],
                'operator' => 'NOT IN',
            ),
        );
        return $query_args;
    }
    add_filter( 'woocommerce_shortcode_products_query', 'hide_out_of_stock_items_from_wc_shortcode' );
    
    Please add the above code in functions.php

    This should work for you.

  • dichagr replied

    Arlind, thanks for the reply. You gave me the same code in the previous topic I created as dimissel (dichagr after I changed my username). As I said there, it's working for the laborator shortcodes but it doesn't work for the out of stock products inside the product page in the related section.

    If you have any idea why both your code and my code don't work, I would appreciate it.

  • [deleted] replied

    Hi dichagr,

    Please add this code in addition to the previous one:

    function _aurum_hide_out_of_stock_products_from_related_products( $value ) {
        remove_filter( 'pre_option_woocommerce_hide_out_of_stock_items', __FUNCTION__ );
        return 'yes';
    }
    add_action( 'woocommerce_output_related_products_args', function() {
        add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', '_aurum_hide_out_of_stock_products_from_related_products' );
    } );
    
    This should do the trick.

  • dichagr replied

    Out of stock products are still appearing, and it also really messes up with my CSS.

    Anyway, I guess there is no way to hide them, I give up. Thanks for the help, I really appreciate it

  • [deleted] replied

    Ok then.

    Will mark this ticket as solved from here.