How To Loop Through WooCommerce Categories

Looking to loop through WooCommerce categories outside the products page in WordPress? All you may need is to use get_categories() in a foreach PHP loop.

Looping through WooCommerce categories

To loop through WooCommerce categories outside the products page in WordPress, use the code below.

<?php
$args = array(
     'taxonomy' => 'product_cat',
     'orderby' => 'name',
     'order' => 'ASC',
     'hide_empty' => false
);
foreach( get_categories( $args ) as $category ) :
     //do category data
endforeach;
?>

Excluding WooCommerce categories

To exclude certain categories, you can pass the exclude argument through the argument’s array.

$args = array(
     'taxonomy' => 'product_cat',
     'orderby' => 'name',
     'order' => 'ASC',
     'hide_empty' => false
     'exclude' => array(1, 2, 3) //category IDs to exclude
);