Woocommerce:当产品不可见时隐藏子类别

时间:2016-09-24 18:05:52

标签: php wordpress loops woocommerce

我写了这个代码,如果它们不为空,则显示我的所有类别和子类别。但是,某些子类别会显示,因为其中有隐藏的项目。所以从本质上讲,不是空的,也不是可见的。我想排除这些..

function product_subcategories( $args = array() ) {

  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 1;

  $args = array(
     'taxonomy'     => $taxonomy,
     'orderby'      => $orderby,
     'show_count'   => $show_count,
     'pad_counts'   => $pad_counts,
     'hierarchical' => $hierarchical,
     'title_li'     => $title,
     'hide_empty'   => $empty
  );

 $all_categories = get_categories( $args );

 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       
        echo '<ul class="parent"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

          $args2 = array(
                  'taxonomy'     => $taxonomy,
                  'child_of'     => 0,
                  'parent'       => $category_id,
                  'orderby'      => $orderby,
                  'show_count'   => $show_count,
                  'pad_counts'   => $pad_counts,
                  'hierarchical' => $hierarchical,
                  'title_li'     => $title,
                  'hide_empty'   => $empty,
                  'meta_query'   => array(
                                        array( 
                                        'key' => '_visibility',
                                        'value' => 'hidden', 
                                        'compare' => '!=', 
                                    ))
          );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
              echo  '<li class="sub"><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>'; 
              }
        }
        echo '</ul>';
    }       
  }
}
add_action( 'categories', 'product_subcategories', 50 );

我尝试做的是添加元查询检查以查看产品是否未标记为隐藏&#39; :

              'meta_query' => array(
                                  array( 
                                  'key' => '_visibility', 
                                  'value' => 'hidden', 
                                  'compare' => '!=', 
                              ))  

这绝对不会返回任何类别。有什么想法吗?

0 个答案:

没有答案