隐藏Woocommerce商店页面上的所有促销产品 [英] Hide all on sale products on Woocommerce shop page
问题描述
是否可以在标准WooCommerce商店页面上隐藏所有标记为销售"的产品?
Would it be possible to hide all the products marked as "sale" on the standard WooCommerce shop page?
销售产品将发布在单独的销售"页面上.
The sale products will be posted on a separate Sale page.
推荐答案
要在Woocommerce商店页面上隐藏所有销售产品 (可变产品除外) ,请使用:
To hide all on sale products (except variable products) on Woocommerce shop page, use:
add_filter( 'woocommerce_product_query_meta_query', 'on_sale_products_not_in_archives', 10, 2 );
function on_sale_products_not_in_archives( $meta_query, $query ) {
// For woocommerce shop pages
if( is_shop() ){
$meta_query[] = array(
'key' => '_sale_price',
'value' => '',
'compare' => '=',
);
}
return $meta_query;
}
代码进入您的活动子主题(或主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or theme). Tested and works.
如果需要在所有归档页面上将其用作产品类别归档页面和产品归档标签页面,请使用:
If you need that on all archives pages as product category archive pages and product archive tag pages, use:
if( is_shop() || is_product_tag() || is_product_category() ){
而不是if( is_shop() ){
…
对于可变产品,这是不可能的,因为我们需要检查可变产品的所有产品变体.
For variable products it's not possible as we need to check all the product variations of a variable product.
这篇关于隐藏Woocommerce商店页面上的所有促销产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!