Created
April 8, 2020 16:55
-
-
Save geotsiokos/41aa1c772f0be9edf563880005824ba6 to your computer and use it in GitHub Desktop.
Display WC categories/subcategories on shop and archive templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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