Hide out of stock products only on shop archive pages in Woocommerce

前端 未结 1 547
长情又很酷
长情又很酷 2020-12-11 09:49

I am trying to hide out of stock products only from Shop page, but keep them on the separate category page.

Woocommerce settings allows only to choose either to show

相关标签:
1条回答
  • 2020-12-11 10:30

    Updated: Using a custom function hooked in woocommerce_product_query_meta_query filter hook, targeting non "out of stock" products on shop archive pages only:

    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_shop() ) return $meta_query;
    
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!='
        );
        return $meta_query;
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works

    0 讨论(0)
提交回复
热议问题