简体   繁体   English

从Woocommerce的商店页面中排除产品类别

[英]Excluding product categories from shop page in Woocommerce

I am trying to hide a product category from my product category menu. 我正在尝试从产品类别菜单中隐藏产品类别。

    add_filter( 'woocommerce_product_categories_widget_args', __NAMESPACE__ . '\\rv_exclude_wc_widget_categories' );
    function rv_exclude_wc_widget_categories( $cat_args ) {
        $cat_args['exclude'] = array('129'); // Insert the product category IDs you wish to exclude
        $includes = explode(',', $cat_args['include']); 
        $cat_args['include'] = array_diff($includes,$cat_args['exclude']);
        return $cat_args;
    }

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1, -129' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

The top function of my code successfully hides the category from my menu when actually in a category. 我的代码的顶部功能实际上在类别中时成功地从菜单中隐藏了该类别。 However, it does not hide it from the main shop page. 但是,它不会从商店主页面隐藏它。 This is what I'm attempting with the bottom code, however, this doesn't appear to do anything. 这就是我尝试使用的底部代码,但是,这似乎没有任何作用。

Any ideas on how this can be done? 关于如何做到这一点的任何想法? The code is placed in my functions.php file. 该代码位于我的functions.php文件中。

EDIT: Trying to clarify what I'm asking. 编辑:试图澄清我在问什么。

When first opening my product page I now have the category 'TEST' hidden from my menu like below. 第一次打开我的产品页面时,我现在从菜单中隐藏了“测试”类别,如下所示。

在此处输入图片说明

However, when I click into a product or category the menu goes back to displaying like below. 但是,当我单击产品或类别时,菜单返回到如下所示的显示。

在此处输入图片说明

For your first function (hiding a specific product category from widget) : 对于您的第一个功能(在小部件中隐藏特定的产品类别)

add_filter( 'woocommerce_product_categories_widget_args', 'exclude_product_categories_widget', 10, 1 );
function exclude_product_categories_widget( $list_args ) {
    $categories = array('129');

    if(isset( $list_args['include'])):
        $included_ids =  explode( ',', $list_args['include'] );
        $included_ids = array_unique( $included_ids );
        $included_ids = array_diff ( $included_ids, $categories );

        $list_args['include'] = implode( ',', $included_ids);
    else:
        $list_args['exclude'] = $categories;
    endif;

    return $list_args;
}

To exclude your products under product category (term ID) 129 in shop and archive pages use the following dedicated Woocommerce hooked function: 要在商店和归档页面中排除产品类别(术语ID) 129下的产品,请使用以下专用的Woocommerce挂钩函数:

add_filter('woocommerce_product_query_tax_query', 'exclude_product_category_in_tax_query', 10, 2 );
function exclude_product_category_in_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;

    // HERE Define your product categories Terms IDs to be excluded
    $terms = array( 129 ); // Term IDs

    // The taxonomy for Product Categories custom taxonomy
    $taxonomy = 'product_cat';

    $tax_query[] = array(
        'taxonomy' => $taxonomy,
        'field'    => 'term_id', // Or 'slug' or 'name'
        'terms'    => $terms,
        'operator' => 'NOT IN', // Excluded
        'include_children' => true // (default is true)
    );

    return $tax_query;
}

Code goes in function.php file of your active child theme (or theme). 代码进入您的活动子主题(或主题)的function.php文件中。 Tested and works. 经过测试和工作。

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

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