Problem Description:

WooCommerce has a default number of 2 and 1 for related products and upsells. In some themes, we have added a custom function that increases this number, but you may want to customize this anyway.

Answer:

This is considered a modification, and is provided as-is. We expect you understand what you are doing if applying coding changes using these or other WordPress tutorials. Make a backup of your existing file so you can replace it if something goes wrong and you need to start over. Modified files WILL probably be replaced by a theme update at some point, so keep note of your changes for future reference or see our guides below. We cannot assist you with modifying your theme or support problems you encounter as a result!

If you are modifying a theme as part of a client project, the following are really important!

How to Modify Your Theme the Right Way
How to Track Update Changes and Maintain Modifications

 

  1.  Open the functions.php file for your child theme and add the following code to the bottom
  2. If your file has a ?> at the end, remove it and any blank lines at the end of the document (the closing bracket of this code should be the last thing in the file)
  3. Alternatively, create a new file such as woocommerce.php and add it to your child-theme’s ocmx/theme-setup folder.
    • Do not add these functions to your parent theme in a new file or it may cause a function conflict!
/**
* WooCommerce Extra Feature
* --------------------------
*
* Change number of related products on product page
* Set your own value for 'posts_per_page'
*
*/// Redefine wooCommerce related products
 
function woocommerce_output_related_products() {
woocommerce_related_products(3,1); // Display 3 products in rows of 3
}
 
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
 
    if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
        function woocommerce_output_upsells() {
        woocommerce_upsell_display( 3,1 ); // Display 3 products in 1 row
    }
}
/** Change number of upsells in single prodict posts
*  Set the first number to how many total and the second number to how many rows.
*/
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
 
    if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
        function woocommerce_output_upsells() {
        woocommerce_upsell_display( 3,1 ); // Display 3 products in 1 row
    }
}

In each function, the number of images and columns is defined inside the display brackets (3,1)

3 products in 1 row is ideal to the theme’s design.

Make sure there is not a blank line at the bottom of the file, then save and upload your changes.