[英]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. Woocommerce 设置只允许选择显示缺货产品或完全隐藏它们。
How to hide out of stock products only on shop archive pages in Woocommerce?如何仅在 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:更新:使用挂在
woocommerce_product_query_meta_query
过滤器钩子中的自定义函数,仅针对商店存档页面上的非“缺货”产品:
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.此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
Tested and works测试和工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.