Skip to content

Instantly share code, notes, and snippets.

@geotsiokos
Created April 8, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotsiokos/41aa1c772f0be9edf563880005824ba6 to your computer and use it in GitHub Desktop.
Save geotsiokos/41aa1c772f0be9edf563880005824ba6 to your computer and use it in GitHub Desktop.
Display WC categories/subcategories on shop and archive templates
add_action( 'woocommerce_before_shop_loop', 'example_woo_list_categories' );
/**
* Action Callback
*/
function example_woo_list_categories() {
$result = false;
$output = '';
if ( is_product_category() ) {
$queried_object = get_queried_object();
$parent = $queried_object->term_id;
$result = true;
} else if ( is_shop() ) {
$parent = 0;
$result = true;
} else {
$result = false;
}
if ( $result ) {
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0,
'parent' => $parent
);
$categories = get_categories( $args );
$output .= '<div class="subcats-list">';
$output .= '<ul class="subcategories">';
foreach ( $categories as $cat ) {
$subcategory = get_term_by( 'id', $cat->term_id, 'product_cat' );
if ( $subcategory && $subcategory->count != 0 ) {
$output .= '<li class="subcategory">';
$output .= '<a target="_self" href="' . esc_url( get_term_link( $cat, 'product_cat' ) ) . '">';
$output .= $subcategory->name;
$output .= '</a>';
$output .= '</li>';
}
}
$output .= '</ul>';
$output .= '</div>';
}
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment