Reply 189707 to: Show related products by same category

Avatar: Rose Tyler
Rose Tyler
Support staff
March 29, 2018 at 15:46

Hello,

Unfortunately, we can’t connect to your server via FTP.
But please change the previous code to https://prnt.sc/iy2jak

$cats_array = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'ids'));
$sub_cats = array ();
for ( $i =0; $i < count($cats_array); $i++ ) {
	$args = array(
       'hierarchical' => 1,
       'show_option_none' => '',
       'hide_empty' => 0,
       'parent' => $cats_array[$i],
       'taxonomy' => 'product_cat'
    );
    foreach (get_categories($args) as $key) {
    	$sub_cats[] = $key->term_id;
    }
}
$args = apply_filters( 'woocommerce_related_products_args', array(
	'post_type'            => 'product',
	'ignore_sticky_posts'  => 1,
	'no_found_rows'        => 1,
	'posts_per_page'       => $posts_per_page,
	'orderby'              => $orderby,
	'post__in'             => $related,
	'post__not_in'         => array( $product->get_id() ),
	'tax_query' => array(
		array(
			'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $sub_cats
		)
	)
) );

Regards

Go To The Whole Conversation In Topic
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.