-
-
Save woogist/5975638 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* WooCommerce Extra Feature | |
* -------------------------- | |
* | |
* Change number of related products on product page | |
* Set your own value for 'posts_per_page' | |
* | |
*/ | |
function woo_related_products_limit() { | |
global $product; | |
$args['posts_per_page'] = 6; | |
return $args; | |
} | |
add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' ); | |
function jk_related_products_args( $args ) { | |
$args['posts_per_page'] = 4; // 4 related products | |
$args['columns'] = 2; // arranged in 2 columns | |
return $args; | |
} |
This code shows always the same products. Any suggestion how to fix it?
It doesn't make any sense to mess up with styles while there's argument "columns" that should do the job. It just doesn't work, that's all.
Here's a quick and dirty way to get more products on related products:
- Make these directories in your child theme:
/woocommerce/single-product
(* if you are new to child themes read dooumentation here: https://codex.wordpress.org/Child_Themes)
- Copy the file related.php from this directory:
/wp-content/plugins/woocommerce/templates/single-product
and Paste it into the directory you made in step 1
- In the new related.php in your child theme open the file and change this line:
'posts_per_page' => $posts_per_page //change this var to how many related products appears.
eg. 'post_per_page' => 6;
Hope this works for you guys.
Thanks issac! Works!
the easiest and best way I found was by using a plugin called Woocommerce booster!, it works like charm. http://wooassist.com/how-to-change-the-number-of-related-products-in-woocommerce/
For me the following worked by setting the priority parameter:
add_filter( 'woocommerce_output_related_products_args', 'cwc_change_number_related_products', 20 );
function cwc_change_number_related_products( $args ) {
$args['posts_per_page'] = 2; // # of related products
$args['columns'] = 2; // # of columns per row
return $args;
}
Thanks Derek, that did the trick with priority. So, I wanted to change the number of products per row and ended up with this:
/* ADD 4 RELATED PRODUCTS */
add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args', 20 );
function jk_related_products_args( $args ) {
$args['posts_per_page'] = 4; // 4 related products
$args['columns'] = 4; // arranged in 4 columns
return $args;
}
I figured I'd post it here in case someone else is looking to do the same.
+1 for suggesting to add the priority to the add_filter() function. Got it working for me!
add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args', 20 );
Yes the number of elements change, but the number of columns don't. We can control that by CSS though.
Like For 4 Columns:
.woocommerce .related ul li.product {
width: 22% !important;
clear: none;
margin-right: 4%;
}
.woocommerce .related ul li.product:nth-of-type(4n) {
margin-right: 0;
}