Website is always display for products, even if I add this code to FUNCTIONS.PHP:
/** Change number of related products output */
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', 20 );
function jk_related_products_args( $args ) {
$args['posts_per_page'] = 6;
$args['columns'] = 6;
return $args;
}
Hey Dani,
Are you already displaying related products on your product pages from Theme Options? Can you post a link?
Thanks!
Hannah
Can you confirm that there are more than 4 related products (products within the same category/tags)?
Hannah
Yes, I confirm that.
You can see that I’m using 6 columns in all sections, but no options to do this with related products.
So Virtue Premium has an option to set the number of columns in the related post carousel to 6, though the free version does not have this option. I will check with the developer to see if he has any advice for you.
Best,
Hannah
Maybe a code snippet to display 6 related products, I can’t change this with any code.
You can use code like this, it’s not really theme related:
add_filter( 'woocommerce_output_related_products_args', 'custom_woo_related_products_limit', 20 );
function custom_woo_related_products_limit( $args ) {
$args['posts_per_page'] = 6;
$args['columns'] = 6;
return $args;
}
Ben
Thank you, but it doesn’t work.
See customized RELATED.PHP in /MYTHEME/SINGLE-PRODUCT directory:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
// Get ID of current product, to exclude it from the related products query
$current_product_id = $product->get_id();
$cats_array = array(0);
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'post__not_in' => array( $current_product_id ), // exclude current product
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<section class="related products">
<?php
$heading = apply_filters( 'woocommerce_product_related_products_heading', __( 'Related products', 'woocommerce' ) );
if ( $heading ) :
?>
<h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php endif;
wp_reset_postdata();
I tested that code using virtue and woocommerce. It works for me.
I don’t suggest you edit the related.php file. There is no need if you just want to change columns and limit.
If you are going to edit the php file I suggest using the latest version from woocommerce: https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/related.php
Using a customized RELATED.PHP is for restrict related only for current single product subcategory, excluding current product.
I suggest you do that through a filter rather then overriding a template file.
However if you want to override just remove the filter for “woocommerce_related_products_args” so you don’t have anything overriding it.
For example in your file change this:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'post__not_in' => array( $current_product_id ), // exclude current product
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
));
To this:
$args = array(
'post_type' => 'product',
'post__not_in' => array( $current_product_id ), // exclude current product
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
);
Any filter to add to FUNCTIONS.PHP in order to retrieve only related from same subcategory, using default Woocommerce related code?
You can override the related products query using the filter here: https://github.com/woocommerce/woocommerce/blob/bdb1451f2b682ded4b7e581e5fcffcc2235feafe/includes/data-stores/class-wc-product-data-store-cpt.php#L1244
That would allow you to define your own query from the functions file without the need for overriding the template to do so.
You can learn more about filters in woocommerce here: https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
All the best,
Ben