• Resolved Tim Burkart

    (@bigmoxy)


    I am using a filter to hide out of stock products however when I inserted the All Products block on the home page the filter does not work for that block. I tried using the is_front_page condition but that does not work. Is there another condition I should use, or another filter altogether?

    add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
    function shop_only_instock_products( $meta_query, $query ) {
        // Only on shop archive pages
        if( is_admin() || is_search() || ( !is_front_page() && !is_shop() && !is_product_category() && !is_product_tag() ) ) return $meta_query;
    
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!='
        );
        return $meta_query;
    }
    

    Thank you!

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @bigmoxy,

    Out of stock products are hidden in the All Products block when the option “Hide out of stock items from the catalog” next to Out of stock visibility within WooCommerce > Settings > Products > Inventory is enabled. Doesn’t this work for you?

    Thanks.

    Thread Starter Tim Burkart

    (@bigmoxy)

    Hi @rainfallnixfig,

    Thank you for your reply. I cannot use that setting because my client wants a page dedicated to displaying out of stock products. If I enable the setting, the dedicated out of stock products page no longer works. Do you have a suggestion on how I could change this filter to display out of stock products even when the hide setting is enabled?

    add_shortcode( 'out_of_stock_products', 'rompre_out_of_stock_products_shortcode' );
    function rompre_out_of_stock_products_shortcode() {
     
       $args = array(
          'post_type' => 'product',
          'posts_per_page' => -1,
          'post_status' => 'publish',
          'meta_query' => array(
             array(
                'key' => '_stock_status',
                'value' => 'outofstock',
             )
          ),
          'fields' => 'ids',
       );
        
       $product_ids = get_posts( $args ); 
       $product_ids = implode( ",", $product_ids );
        
       return do_shortcode("[products ids='$product_ids']");
     
    }
    Mirko P.

    (@rainfallnixfig)

    Hi @bigmoxy,

    Thanks for clarifying and for sharing your snippet.

    I’m going to leave this thread open for a bit to see if anyone is able to chime in to help you out.
    ?
    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack.

    We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter Tim Burkart

    (@bigmoxy)

    Received additional assistance from businessbloomer.com.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to hide out of stock products using all products block’ is closed to new replies.