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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=17473
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). 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+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

32 thoughts on “WooCommerce: Change Number of Related Products

  1. 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!

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

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

    1. Nope, sorry

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

  5. 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 πŸ™‚

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

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

    All the best to you!
    Jelena

    1. Brilliant πŸ™‚

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

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

  10. 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!

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

  12. 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!

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

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

  15. 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? Support? Leave your Comment Now!
_____

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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.