WooCommerce: Change Number of Related Products

Here’s a simple snippet to change the display of Related Products on the single product page. Lately, I even added a fix for the Storefront theme, which already overrides the WooCommerce filter and needs a different call.

WooCommerce: Change Number of Related Products
WooCommerce: Change Number of Related Products

Snippet (PHP): Change Number of Related Products (Rows & Cols) – WooCommerce


/**
 * @snippet       WooCommerce Change Number of Related Products
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17473
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );

function bbloomer_change_number_related_products( $args ) {
 $args['posts_per_page'] = 4; // # of related products
 $args['columns'] = 4; // # of columns per row
 return $args;
}

Additional CSS for Storefront Theme – Change Number of Related Products to 4


@media (min-width: 768px) {
.site-main .related.products ul.products li.product {
    width: 22%;
    float: left;
    margin-right: 4%;
}
}

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 Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • 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: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

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

36 thoughts on “WooCommerce: Change Number of Related Products

  1. The code still works, but I used flex for css.

  2. Fantastic – thank you! That’s one less plugin I need to install πŸ™‚

    1. Yay!

  3. I need to do this with a conditional like:

     
    if ( has_term( 'cat_1', 'product_cat' ) || has_term( 'cat_2', 'product_cat' ) || has_term( 'cat_3', 'product_cat' ) ) {
    add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );
    
    function bbloomer_change_number_related_products( $args ) {
     $args['posts_per_page'] = 9; // # of related products
     $args['columns'] = 3; // # of columns per row
     return $args;
    }
    }

    but it doesn’t work, Any suggestions?
    Thanks in advance!

    1. I have it, the conditional must be inside the function like this:

      add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );
       
      function bbloomer_change_number_related_products( $args ) {
      if ( has_term( 'cat_1', 'product_cat' ) || has_term( 'cat_2', 'product_cat' ) || has_term( 'cat_3', 'product_cat' ) ) {
           $args['posts_per_page'] = 9; // # of related products
           $args['columns'] = 3; // # of columns per row
           return $args;
           }
      }
      

      In case it serves to someone πŸ™‚

      1. Nice!

  4. How can I display 6 products in single row? It is showing 4+2 in 2 rows

    1. Hello Paul, thanks for your comment. If you entered “6” inside the snippet, then it’s your theme’s fault if that’s the result. You’ll need to add custom CSS to make the output show on 6 columns. Hope this helps

  5. Does this works on the cross sell on the cart page??

    1. Nope, sorry

  6. With WP core 5,1, woocommece 3.5.6 and storefront theme 2.4.3 the number of products shown are fix to 2. The CSS part works fine making room for more, but only two products are shown (from left). I have also Pootlepress Storefront Pro installed.

    1. Works for me πŸ™‚ If you disable temporarily all plugins does it work?

  7. Today – feb 6th 2019 – the setting for the number of related items works, however, the column setting does not.
    Just #fyi

    1. Hey Han, thanks for your comment! Snippet works for me, so you must have a plugin/theme conflict πŸ™‚

  8. Hello,

    Nice work Rodolfo congratulations. I would like to ask if it’s possible to have random related products from the same category and subcategory by using ‘orderby’ => ‘rand’, that kind of argument.

    Thank you in advance,

    Nikos

    1. Hello Nikos, thanks for your comment πŸ™‚ “Orderby” = “rand” is already the default argument, check woocommerce_output_related_products(). so, it should already work like that πŸ™‚

    2. Hello Rodolfo,

      Of course it is working like that by default. What it doesn’t do as it should or better as we want to do is showing related products from the same subcategory and not from the parent category.

      Thank you in advance,

      Nikos

      1. Ah, gotcha! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  9. Thank you SO much, man! You just saved my day πŸ™‚

    All the best to you!
    Jelena

    1. Brilliant πŸ™‚

  10. How do I change the no.of items per row for Up Sells and Cross sells please.

    1. Hello Geetha, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  11. Wow, Jesse! Thank you very much for this!
    Since yesterday I am trying to get more than the usual 3 images to be displayed. Lot of testing with frustrating results. I didn’t expect Storefront to be the cause. If Google would’ve paid me for each single search I did the last days, I would be rich by now. πŸ™‚ Luckily I decided to search for storefront specifically.

    Thank you again, all the best to you,

    -BjΓΆrn

  12. Hi does this still work? Also, does it change the number of upsell products displayed from 3 to 4? I’m running the Divi theme.

    Thanks

    1. Hey Lee! Try adding a “9999” priority to the filter so it loads last (I revised the snippet). On top of that, your theme might require additional CSS to manage the WooCommerce columns (see Storefront example). Finally, this only changes the related products, for the upsells the code would be different. Hope this helps!

  13. Thumbs up! StoreFront snippet works like a charm!

  14. I have spent hours looking for a fix. I was trying to display 6 related products in 2 rows of 3 but it wouldn’t display with 3 per row. I would get 2 on one row and then 1 or 2 on the second row depending on how many characters there were in the excerpt. Your Related Products code has fixed this problem instantly. Thank you so much!

  15. Hi,

    one more time, amazing – including adaptation for storefront.

    Question: any idea how to make the related products displayed only stock products ?
    on my shop it shows almost only sold out products…

    Thanks a lot πŸ™‚

    1. Ben, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R

  16. Hey, any idea how to change the number of related products in StoreVilla theme ?

    Thanks in advance!

    1. No idea Weber πŸ™‚ Have you asked your theme developers?

  17. Just FYI, it looks like your Storefront version is exactly the same as your other version except for the function name. I believe you’ll have to give the filter a priority of at least 11 to have it work.

    1. Jesse, spot on! Thanks so much for your comment, I must have forgotten the priority for some obscure reason πŸ™‚ Have a great day!

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 *