从Woocommerce的商店页面中排除可下载产品 [英] Exclude downloadable products from shop pages in Woocommerce

查看:101
本文介绍了从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' => '=', ...

这篇关于从Woocommerce的商店页面中排除可下载产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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