How To Get WooCommerce Product Categories

5/5

The World's No.1 WooCommerce Plugin

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 '<ul class="wc-product-categories-list">';

	 foreach ( $all_categories as $cat ) {

	    if ( $cat->category_parent == 0 ) {

	        $category_id = $cat->term_id;    

	        echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

	        /**
	         *
	         *	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 '<ul class="wc-sub-categories-list">';

	            foreach( $sub_cats as $sub_category ) {

	                echo  '<li>><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';

	            }

	            echo '</ul>';

	        }

	    }  

	}

	echo '</ul>';
				
			

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 '<ul class="wc-product-categories-list">';

	foreach ( $all_categories as $cat ) {

		if ( $cat->category_parent == 0 ) {

			$category_id = $cat->term_id;    

			echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

			/**
			 *
			 *	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 '<ul class="wc-sub-categories-list">';

			    foreach( $sub_cats as $sub_category ) {

			        echo  '<li>><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';

			    }

			    echo '</ul>';

			}

		}  

	}

	echo '</ul>';

	return ob_get_clean();

}
				
			
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Akbar
Akbar
12 hours ago

Great, it work for me. Thanks