Hello Summer Sale!
Exclusive season-opening discount on TheGem theme.
Limited time offer.

Okay
  Public Ticket #1389773
Woocommerce: change number of related products
Closed

Comments

  •  1
    Pompoen started the conversation

    Hi,

    Is there an option to set the number of related products shown at the bottom of a product page? Now there are 6 but I want to change that to 4.

    Thanks!

  • [deleted] replied

    Hi,

    Please read this article - https://docs.woocommerce.com/document/change-number-of-related-products-output/.

    Our theme use this hook:

    function thegem_woocommerce_output_related_products_args($args) {
         $args['posts_per_page'] = 6;
         $args['columns'] = 6;
         return $args;
    }
    add_filter('woocommerce_output_related_products_args', 'thegem_woocommerce_output_related_products_args');
    You can try to change this with same code in child theme.

  •  1
    Pompoen replied

    Hi Max,


    Thanks. I found that article, but it didn't seem to work for me. I've tried your code but there's an error:

    Your PHP code changes were rolled back due to an error on line 204 of file wp-content/themes/thegem/inc/woocommerce.php. Please fix and try saving again.

    Cannot redeclare thegem_woocommerce_output_related_products_args() (previously declared in wp-content/themes/thegem-child/functions.php:26)

    When I look open woocommerce.php in thegem/inc and look at line 204 that that's indeed the line to set the number of posts and columns for the related products. I can change it there and that will work, but I'd obviously rather add a line in my functions.php in my child theme. Or would it be an option to put a woocommerce.php file with this code in a thegem-child/inc folder, to prevent it from rolling back after an update?

  • [deleted] replied

    You need to use code like this:

    function thegem_child_woocommerce_output_related_products_args($args) {
         $args['posts_per_page'] = 4;
         $args['columns'] = 4;
         return $args;
    }
    add_filter('woocommerce_output_related_products_args', 'thegem_child_woocommerce_output_related_products_args', 11);