WooCommerce: Split Shop Page By Category

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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_no_products_found', 'bbloomer_show_4_products_per_category' );

function bbloomer_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 '<hr><h2>' . $term_object->name . '</h2>';
		echo do_shortcode( '[products limit="4" columns="4" category="' . $category_slug . '"]' );
		echo '<p><a href="' . get_term_link( $category_slug, 'product_cat' ) . '">View all ' . $term_object->name . ' products &rarr;</a>';
	}
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

11 thoughts on “WooCommerce: Split Shop Page By Category

  1. I used your split shop page by categories, but I would also like to have the categories list appear above the products. I.e., shop page is categories+products, but when this is set, the products don’t appear with your snippet active. How can I have my categories appear above the products loop, and have my page in the products in categories layout? Thanks.

    1. Gotcha. Try using this instead:

      add_action( 'woocommerce_after_shop_loop', 'bbloomer_show_4_products_per_category', 9 );
      1. Thanks Rodolfo,

        I tried this and it doesn’t quite do what I want. It lists the product categories after the shop, not before, and I’d like the list to be inline with the category images.

        1. Sorry, needed to go back to customise and reset products+categories. Works fine. Many thanks.

          K

  2. price widget in sidebar will not work if use shortcode, we should customize WooCommerce_shop_loop for that

    1. Thanks for that – surely my solution doesn’t fit all scenarios. Did you try the official plugin instead?

  3. Hi, It also appears that the attribute filter do not work when tge snippets are installed.

    Thanks Again

    1. Thanks Mark – you refer to the sidebar filter widget? Do you have a screenshot?

  4. worked well, no hassle. no classes on view all button. would also like to be able to control which categories and which order they appear. Any more help much appreciated.

    Your newsletter is still the only WooCommerce one i read.

    Thanks for above

    Mark

    1. Hey Mark, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *