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!
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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 →</a>';
}
}
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.
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?
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.
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?
Hello Rodolfo,
How can we sort the products? Should I add an arg to the do_sortcode?
Correct. You can study the official WooCommerce shortcode docs and that should help
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.
Gotcha. Try using this instead:
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.
Sorry, needed to go back to customise and reset products+categories. Works fine. Many thanks.
K
Cool
price widget in sidebar will not work if use shortcode, we should customize WooCommerce_shop_loop for that
Thanks for that – surely my solution doesn’t fit all scenarios. Did you try the official plugin instead?
Hi, It also appears that the attribute filter do not work when tge snippets are installed.
Thanks Again
Thanks Mark – you refer to the sidebar filter widget? Do you have a screenshot?
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
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!