display related product of same subcategory in woocommerce
Modify related.php template file. PATH: your-theme/woocommerce/single-product/related.php
Change $args to,
$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__not_in' => array( $product->id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
)
) );
where $cats_array is,$cats_array = array(0);
// Get product categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
//Select only the category which doesn't have any children
if( sizeof( $terms ) ){
foreach ( $terms as $term ) {
$children = get_term_children( $term->term_id, 'product_cat' );
if ( !sizeof( $children ) )
$cats_array[] = $term->term_id;
}
}
and remove/comment following lines,$related = $product->get_related( $posts_per_page );
if ( sizeof( $related ) == 0 ) return;
original
Comments
Post a Comment