仅在 Woocommerce 的商店存档页面上隐藏缺货产品 [英] Hide out of stock products only on shop archive pages in Woocommerce

查看:32
本文介绍了仅在 Woocommerce 的商店存档页面上隐藏缺货产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅在商店"页面中隐藏缺货产品,但将它们保留在单独的类别页面上.

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

Woocommerce 设置只允许选择显示缺货产品或完全隐藏它们.

Woocommerce settings allows only to choose either to show out of stock products or to hide them completely.

如何仅在 Woocommerce 的商店存档页面上隐藏缺货产品?

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

推荐答案

更新: 使用挂钩在 woocommerce_product_query_meta_query 过滤器钩子中的自定义函数,针对非缺货"仅限商店存档页面上的产品:

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

此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

经过测试并有效

这篇关于仅在 Woocommerce 的商店存档页面上隐藏缺货产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆