Erik Duits
Erik Duits

Reputation: 111

hide_empty categories in Woocommerce not working

I use this shortcode to display subcategories, and it's working fine.
But i would like to hide empty categories, and hide_empty is not working.

function woocommerce_subcats_by_id( $atts ) {
    extract( shortcode_atts( array(
        'parent_cat_id' => '',
    ), $atts ) );
 
    $idbyid = get_term_by('id', $parent_cat_id, 'product_cat');
    $product_cat_ID = $idbyid->term_id;
      
    $args = array(
        'taxonomy' => 'product_cat',
        'child_of' => $product_cat_ID,
        'hide_empty' => true
        
    );
    $subcats = get_terms($args);
    $content = '<ul class="woodmart-woocommerce-layered-nav">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          $content .= '<li class="woodmart-woocommerce-layered-nav"><a class="layered-nav-link" href="'. $link .'"><span class="swatch-inner"><span class="layer-term-name">'.$sc->name.'</span></span></a></li>';
      }
    $content .= '</ul>';
    return $content;
}
add_shortcode( 'show_sub', 'woocommerce_subcats_by_id' );

Upvotes: 0

Views: 566

Answers (1)

Unce
Unce

Reputation: 27

Although you haven't wrote the beginning of your function. I presume it is like this

function woocommerce_subcats_by_id($atts){

    extract( shortcode_atts( array(
          'parent_cat_id' => '',
    ), $atts ) );
 
    $idbyid = get_term_by('id', $parent_cat_id, 'product_cat');
    $product_cat_ID = $idbyid->term_id;
      
    $args = array(
        'taxonomy' => 'product_cat',
        'child_of' => $product_cat_ID,
        'hide_empty' => true
        
    );
    $subcats = get_terms($args);
    $content = '<ul class="woodmart-woocommerce-layered-nav">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          $content .= '<li class="woodmart-woocommerce-layered-nav"><a class="layered-nav-link" href="'. $link .'"><span class="swatch-inner"><span class="layer-term-name">'.$sc->name.'</span></span></a></li>';
      }
    $content .= '</ul>';
    return $content;
}
add_shortcode( 'show_sub', 'woocommerce_subcats_by_id' );

And It hides the empty categories.

Upvotes: 1

Related Questions