从 Woocommerce 小部件中排除产品类别

问题描述 投票:0回答:2

我们希望从 woocommerce 商店以及所有页面中隐藏/排除一些特定的 woocommerce 类别。

到目前为止,我们使用我在互联网上找到的代码成功实现了这一目标。 下面的代码从商店页面隐藏了正确的类别,但是当我们使用 woocommerve 搜索进行搜索时,该类别不会隐藏在结果页面中。

    //Insert excluded category ids here
    $excludes = array(3380,3308);
    $includes = explode(",",$widget_args['include']);

    $includes = array_filter($includes, function($value) use ($excludes) {
      return !in_array($value, $excludes);
    });
    $widget_args["include"] = implode(",", $includes);
    return $widget_args;
}

add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'exclude_woocommerce_widget_product_categories');
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_woocommerce_widget_product_categories');

下面的代码确实从搜索页面隐藏了类别,但没有从商店页面隐藏类别

add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'organicweb_exclude_widget_category');
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
        $args['exclude'] = array('15', '3380', '3308' );    // Enter the id of the category you want to exclude in place of '30'
        return $args;
}

有人可以帮我将两段代码合并在一起吗?

提前谢谢您。

woocommerce
2个回答
0
投票

您发布的两个代码片段仅隐藏小部件中的类别,否则对隐藏类别没有任何影响。不是 100% 你的目标是什么,在我看来有两件事是可能的:

如果您想从商店页面上的特定类别中排除产品,请使用以下代码执行此操作(如果您选择在后端的“设计”>“定制器”>“WooCommerce”>“产品目录”下显示它们,则不会隐藏类别),如图所示在 WooCommerce 文档中。

/**
 * Exclude products from a particular category on the shop page
 */
function custom_pre_get_posts_query( $q ) {

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

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


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

}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' ); 

要实际隐藏类别本身,请按照此处从商店页面隐藏 WooCommerce 类别中所述执行以下操作,这不会隐藏此类别的产品。

/**
 * Show products only of selected category.
 */
function get_subcategory_terms( $terms, $taxonomies, $args ) {

    $new_terms  = array();
    $hide_category  = array( 126 ); // Ids of the category you don't want to display on the shop page

      // if a product category and on the shop page
    if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
        foreach ( $terms as $key => $term ) {
        if ( ! in_array( $term->term_id, $hide_category ) ) { 
            $new_terms[] = $term;
        }
        }
        $terms = $new_terms;
    }
  return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

0
投票

你好@Nicolai Grossherr。您的第二个代码效果很好,但是一旦用户单击链接到任何隐藏类别的任何页面,所有隐藏类别都会出现,从而使页面无法呈现。是否可以修改它以防止在选择隐藏类别后出现隐藏类别?谢谢。下面是代码

    /**
 * Show products only of selected category.
 */
function get_subcategory_terms( $terms, $taxonomies, $args ) {

    $new_terms  = array();
    $hide_category  = array( 126 ); // Ids of the category you don't want to display on the shop page

      // if a product category and on the shop page
    if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
        foreach ( $terms as $key => $term ) {
        if ( ! in_array( $term->term_id, $hide_category ) ) { 
            $new_terms[] = $term;
        }
        }
        $terms = $new_terms;
    }
  return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
© www.soinside.com 2019 - 2024. All rights reserved.