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
 * @community     https://businessbloomer.com/club/
 */

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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: How to Remove the “Default Sorting” Dropdown
    If the WooCommerce product sorting functionality (“Default Sorting” dropdown) is a waste of space or you don’t need that select box at all, you may want to remove it. No matter if you prefer custom code or a simple plugin – hiding the product sorting dropdown is a piece of cake. Enjoy!

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. Follow @rmelogli

17 thoughts on “WooCommerce: Split Shop Page By Category

  1. Hello Rodolfo,
    Product search does not work when the snippets are installed. It keeps showing the split category view on the shop page when searching for product.
    Attributes filter does not work on the main shop page, however, it does on other category archives.
    Think we could have a solution for search to work?
    thanks.

    1. Good point. I’ve now added a new conditional to https://www.businessbloomer.com/woocommerce-remove-loop-shop-page/ and made sure it does not run on the search result page. Can you let me know if it works properly?

  2. Yeah, I don’t know why but it’s not doing anything at all for me. I added all 3 snippets to my functions and it’s not changing in the slightest. Weird. If you want more info just email me. BTW, I’m currently going through ensuring nothing is conflicting but I don’t know what could override that.

    1. Could be that your theme completely overrides the WooCommerce templates, and therefore standard hooks don’t work. Mind switching to a default theme temporarily to see if I’m right?

  3. Hello Rodolfo,

    How can we sort the products? Should I add an arg to the do_sortcode?

    1. Correct. You can study the official WooCommerce shortcode docs and that should help

  4. 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

  5. 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?

  6. 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?

  7. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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