简体   繁体   English

Woocommerce 从商店页面中排除某些类别

[英]Woocommerce Excluding Certain Category from Shop Page

Trying to exclude a single category from my WooCommerce's Shop Page试图从我的 WooCommerce 商店页面中排除一个类别

I was using this code but it would break my sites's attributes filter links:我正在使用此代码,但它会破坏我网站的属性过滤器链接:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    if ( ! is_admin() && is_shop() ) {

        $q->set( 'tax_query', array(array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
        )));

     }

     remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

This solution excluded the category from Shop Page.此解决方案从商店页面中排除了类别。 But the problem is, the Category archive page for the excluded category says no products available.但问题是,排除类别的类别存档页面显示没有可用的产品。

I've E-Books category which I don't want to enlisted on Shop Page.我有不想在商店页面上登记的电子书类别。 I have created a separate menu item for E-Books, which is where I want to list all books from the E-Books category.我为电子书创建了一个单独的菜单项,我想在这里列出电子书类别中的所有书籍。

How to achieve this?如何实现这一目标?

Update更新

I added if (!$q->is_main_query() || !is_shop()) return;我添加了if (!$q->is_main_query() || !is_shop()) return; to the action hook and it resolved my above mentioned problem.到动作钩子,它解决了我上面提到的问题。 This line excludes the category only from Shop page, however all products are listed good when the excluded category is accessed directly from menu (category page).此行仅从商店页面排除类别,但是当从菜单(类别页面)直接访问排除的类别时,所有产品都列出良好。

function custom_pre_get_posts_query( $q ) {

if (!$q->is_main_query() || !is_shop()) return;

$tax_query = (array) $q->get( 'tax_query' );

$tax_query[] = array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'ebooks' ),
       'operator' => 'NOT IN'
);


$q->set( 'tax_query', $tax_query );

}

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

You could use the woocommerce_product_query hook, which is pretty similar to pre_get_posts except it already has the appropriate conditional logic.您可以使用woocommerce_product_query钩子,它与pre_get_posts非常相似,只是它已经具有适当的条件逻辑。 There is also a woocommerce_product_query_tax_query filter, but I'm not sure if that is exists in WooCommerce 2.6 or if its new in 2.7 beta.还有一个woocommerce_product_query_tax_query过滤器,但我不确定 WooCommerce 2.6 中是否存在它,或者它是否是 2.7 beta 中的新功能。

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $q->set( 'tax_query', array(array(
       'taxonomy' => 'product_cat',
       'field' => 'slug',
       'terms' => array( 'samples' ), 
       'operator' => 'NOT IN'
    )));

}

EDIT filtering is done through the taxonomy query, and in the above example we're overriding the tax query completely.编辑过滤是通过分类查询完成的,在上面的例子中,我们完全覆盖了税务查询。 I can't test that this works right now (and arrays of arrays are tricky so I may have messed up), but the theory is that we need to merge the new constraint with the existing taxonomy query generated by WooCommerce.我现在无法测试这是否有效(并且数组数组很棘手,所以我可能搞砸了),但理论上我们需要将新约束与 WooCommerce 生成的现有分类查询合并。

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM