【问题标题】:How to exclude product categories in WooCommerce? [duplicate]如何在 WooCommerce 中排除产品类别? [复制]
【发布时间】:2020-07-22 14:56:31
【问题描述】:

如何排除woo 产品类别只在商店前面的功能? 谁能帮帮我?

【问题讨论】:

    标签: woocommerce woocommerce-theming


    【解决方案1】:

    以下代码使用每个类别的 ID 和名称隐藏商店页面和手风琴上的类别。

    对于手风琴,必须使用类别 ID,它将放置在 array ('1', '2'); 的位置,对于商店,您必须使用类别,它将被放置在显示 array ('category-name', 'category-name')

    的位置
    // Hide in Accordion
     
    add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'oaf_wc_exclude_categories_from_list_widget' );
    
    add_filter( 'woocommerce_product_categories_widget_args', 'oaf_wc_exclude_categories_from_list_widget' );
    
    function oaf_wc_exclude_categories_from_list_widget( $cat_args ) {
      
        $cat_args['exclude'] = array('1','2'); // array with categories ids to exclude
        return $cat_args;
    }
    
    // Hide in Store
    
    add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
    
    function get_subcategory_terms( $terms, $taxonomies, $args ) {
    
      $new_terms = array();
    
      // 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->slug, array( 'name-category', 'name-category' ) ) ) {
            $new_terms[] = $term;
          }
    
        }
    
        $terms = $new_terms;
      }
    
      return $terms;
    }
    

    经过测试和工作,代码在您的主题或您的子主题的 fuctions.php 文件中结束。

    这里有一张图片,您可以在其中看到在哪里可以找到类别的 ID 和名称。问候。

    name category, id category

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 1970-01-01
      • 2019-08-05
      • 2018-11-09
      • 2013-08-11
      • 2019-02-06
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多