fetch products based on category id
-
Hello,
I have an issue, I want the woocommerce layout to be like : if a category has sub-categories then display its subcategories else display its products. So, I am editing archive-product.php. The code i wrote is :
<?php
$term = get_queried_object();$children = get_terms( $term->taxonomy, array(
‘parent’ => $term->term_id,
‘hide_empty’ => false
) );// print_r($children); // uncomment to examine for debugging
if($children) {
woocommerce_subcats_from_parentcat_by_ID($term->term_id);
/*above function is actually present in functions.php*/
}
else {
$args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 99, ‘parent’ => $term->term_id, ‘orderby’ => ‘rand’ );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class=”product col-md-3″>
post->ID ) ?>” title=”<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>”><?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<p><?php the_title(); ?></p>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, ‘shop_catalog’); else echo ‘‘; ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query();}
?>
The first if condition is working fine. But, the second part is showing all the products. But I want products of a particular category (based on its id :
$term->term_id). Please, help me.Regards,
Usha
- The topic ‘fetch products based on category id’ is closed to new replies.