functions – How to hide out of stock products using all products block on home page

Question

I am using the filter below to hide out of stock products from various queries. Now, I would like to display products on the home page using the all products block. The problem is that the block is showing all products, even the out of stock ones. Can this filter be modified to exclude out of stock products using the all products block?

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;
}

0
Tim Burkart 1 month 2021-10-22T09:23:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse