Get A List Of All WooCommerce Categories Using PHP
It can be useful to put together a list of all product categories you have in your WooCommerce store.
If you know a little bit of PHP, or even if you know how to access your functions.php file, you can use the snippets on this page to create a list of WooCommerce Categories.
Output List Of WooCommerce Categories
The code snippet below will create a list of woocommerce product categories with sub-categories as child list elements below.
You can style the output by using the class selector “wc-product-categories-list”.
You can also get rid of sub categories by getting rid of the sub categories section or by simply deleting line 56.
$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 = 0;
$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 );
echo ' ';
foreach ( $all_categories as $cat ) {
if ( $cat->category_parent == 0 ) {
$category_id = $cat->term_id;
echo '- '. $cat->name .'
';
/**
*
* Show Sub Categories Too
*
*/
$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
);
$sub_cats = get_categories( $args2 );
if ( $sub_cats ) {
echo '';
foreach( $sub_cats as $sub_category ) {
echo '- >'. $sub_category->name .'
';
}
echo '
';
}
}
}
echo '
';
Create Shortcode With List Of WooCommerce Product Categories
It takes very little extra effort to turn the above into a shortcode.
Copy and paste the below snippet into your functions.php file and then you can use a shortcode like this: [wpd-product-categories]
add_shortcode( 'wpd-product-categories', 'wpd_product_categories' );
function wpd_product_categories() {
ob_start();
$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 = 0;
$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 );
echo ' ';
foreach ( $all_categories as $cat ) {
if ( $cat->category_parent == 0 ) {
$category_id = $cat->term_id;
echo '- '. $cat->name .'
';
/**
*
* Show Sub Categories Too
*
*/
$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
);
$sub_cats = get_categories( $args2 );
if ( $sub_cats ) {
echo '';
foreach( $sub_cats as $sub_category ) {
echo '- >'. $sub_category->name .'
';
}
echo '
';
}
}
}
echo '
';
return ob_get_clean();
}
Great, it work for me. Thanks