从Woocommerce的商店页面中排除可下载产品
我正在开发一家同时提供实体产品和可下载产品的Woocommerce商店.我想更改循环的标准查询,以排除所有可下载的产品,但是我找不到解决方法.我希望这些产品仅显示在某个存档页面上,这是没有问题的.
I'm developing a Woocommerce shop with both physical and downloadable products. I would like to alter the standard query for the loop to exclude all downloadable products, but I can't figure out a way to do it. I would like these products to only be displayed on a certain archive page, which is no problem to achieve.
我正在使用pre_get_post对查询进行其他一些更改,但似乎找不到可下载产品的选项.
I'm doing a few other alterations of the query using pre_get_post, but I can't seem to find an option for downloadable products.
我所有的产品都是简单的产品.
All my products are simple products.
任何帮助将不胜感激!
可以使用专用的woocommerce_product_query
Woocommerce动作钩子代替使用pre_get_post
.但是由于可下载产品与元查询有关,因此最好使用woocommerce_product_query_meta_query
Woocommerce专用过滤器挂钩.
Instead of using pre_get_post
, you could use the dedicated woocommerce_product_query
Woocommerce action hook. But as downloadable products are related to a meta query is better to use woocommerce_product_query_meta_query
Woocommerce dedicated filter hook.
它将仅从商店页面删除可下载的产品:
It will remove downloadable products from shop page only:
add_filter( 'woocommerce_product_query_meta_query', 'downloadable_not_in_shop', 20, 2 );
function downloadable_not_in_shop( $meta_query, $query ) {
// Only on archive pages
if( is_admin() || ! is_shop() )
return $meta_query; // exit
$meta_query[] = array(
'key' => '_downloadable',
'value' => 'yes',
'compare' => '!=',
);
return $meta_query;
}
代码进入您的活动子主题(或主题)的function.php文件中.经过测试并可以正常工作.
要仅显示可下载的产品,请改用
'compare' => '=',
...