Home » WooCommerce: Split Shop Page By Category

WooCommerce: Split Shop Page By Category

by Tutor Aspire

Well, this is gonna be a big one for you. When you start having lots of products and lots of product categories, the shop page becomes either messy or unrepresentative, because it may show just the latest 16 products on page 1 when you have dozens of categories and a much wider range of products…

As usual, there are plugins for that – first one that comes to mind is Nested Category Layout by Skyverge – but today I wanted to see how doable it was to code it, and how many lines of PHP were required.

So, if you wish to switch the WooCommerce shop page display from “products” to “a given number of products for each parent category”, here’s the fix. Enjoy!

With the 3 snippets below, my shop page is now split by categories, and a max of 4 products are displayed on each row, together with a separator, a heading and a link to view more products.

PHP Snippet(s): Display Products By Category @ Shop Page

Snippet 1: Hide default product loop @ Shop Page

If we wish to show products by category, first we need to hide the shop page product display. You can do so with a snippet we already coded at https://www.businessbloomer.com/woocommerce-remove-loop-shop-page/

Snippet 2: Hide “No products were found matching your selection” @ Shop Page

As soon as you tell WooCommerce not to show products, an error message (“No products were found matching your selection”) appears there. Thankfully, we already found a fix to hide it: https://www.businessbloomer.com/woocommerce-hide-no-products-were-found-matching-your-selection/

Snippet 3: Show 4 products per category @ Shop Page

On top of that, I also added a row with the Product Category name wrapped in a H2, and a link to “View all category products” as there may be more than 4 and customers may be interested in refining their search by category.

 * @snippet       4 Products Per Category @ WooCommerce Shop
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://tutoraspire.com

add_action( 'woocommerce_no_products_found', 'tutoraspire_show_4_products_per_category' );

function tutoraspire_show_4_products_per_category() {
$args = array(
'parent' => 0,
'hide_empty' => true,
'taxonomy' => 'product_cat',
'fields' => 'slugs',
$categories = get_categories( $args );
foreach ( $categories as $category_slug ) {
$term_object = get_term_by( 'slug', $category_slug , 'product_cat' );
echo '

' . $term_object->name . '

'; echo do_shortcode( '[products limit="4" columns="4" category="' . $category_slug . '"]' ); echo '

View all ' . $term_object->name . ' products →'; } }

You may also like