How to Create WooCommerce Category Loop Explained With Example

WooCommerce Category Loop
Do you want to understand how the WooCommerce category loop works and how you can leverage it to build unique category pages on your store?

In this post, I will show you how you can use the WooCommerce category loop to display your products and the category details.

If you are creating a custom WooCommerce theme that requires you to display product categories or to display specific products by categories, you need to understand how to create a WooCommerce category loop.

WooCommerce Category Loop

Most of the cases where you want to use the WooCommerce category loop are where you are displaying the categories of products.

You simply need to get all the categories then pick the category you need and get the products added to this category.

When you have this category you can go ahead and loop through the products and display them as needed and using this approach you can create several different category layouts.

In a recent post, I build a custom WooCommerce theme and displayed the categories as shown in the image below.

WooCommerce Category Loop

If you want to achieve this kind of layout on your custom page template, you need need to understand how you can use the WooCommerce category loop and the associated data.

For this layout, you also need to have a plugin that allows you to add category images to the WooCommerce category.

How to Use WooCommerce Category Loop

To use the WooCommerce category loop to display products in a template you need to use the get_categories() function then you loop through the data using the foreach PHP loop.

The following is a code example of how you can use the get_categories() function and the for each loop to display the category data from the WooCommerce database as you wish.

$args = array(
     'taxonomy' => 'product_cat',
     'orderby' => 'name',
     'order' => 'ASC',
     'hide_empty' => false
);
foreach( get_categories( $args ) as $category ) :
     //Display the category data here  
endforeach;

As you can see from the code sample above the way to get the WooCommerce products from the database using the query is to make use of the taxonomy – product_cat.

You can extend the query args to cover different conditions and you can include or exclude the data that you don’t want to be displayed on your page template using the WooCommerce category template.

You can hide the categories that are empty or exclude some categories using the code snippets below :

Hide Categories

$args = array(
    'hide_empty'      => false,
);
get_categories($args); 

Exclude Categories

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

Conclusion

In this post, we have looked at how to get WooCommerce categories and create a WooCommerce category loop that you can use to display products from specific categories on a custom page template.

This code can be useful for your theme development and custom plugin development where you want to use the WooCommerce categories.

Similar Articles