WooCommerce Conditional To Detect When Showing Only Products Archive

Published on 11 November 2016 by John Carter

When viewing an archive of a WooCommerce product category,  there is no way to detect if you are showing an archive of products or an archive of subcategories.

For Example

WooCommerce > Settings > Products Tab

Shop Page Display: Show categories
Default Category Display: Show subcategories

This implies that you want to show the categories on the main shop page, then in each category you want to show subcategories only, until you reach an archive with no more subcategories, where you then just show products.

You add the following function to functions.php to detect if you are showing subcategories or an archive of products:

 

Add a body class to archives that are only showing products (not subcategories):