简体   繁体   中英

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

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 out of stock products or to hide them completely.

How to hide out of stock products only on shop archive pages in Woocommerce?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM