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

戏子无情 提交于 2019-11-27 08:10:35

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/47858352/hide-out-of-stock-products-only-on-shop-archive-pages-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!