• Resolved leoskelter

    (@leoskelter)


    hi I am trying to show all products with its child categories in one page

    – parent category title – (by category id)

    child category name
    > product > product > product > product > product > product

    child category name
    > product > product > product > product > product > product

    child category name
    > product > product > product > product > product > product

    what i have now kinda works

    <?php
    $parentid = get_queried_object_id();
    $args = array( 'parent' => $parentid );
    $terms = get_terms( 'product_cat', $args );?>
    <div class="product-cats-arch">
    	 <?php woocommerce_breadcrumb(); ?>
    <?php if ( $terms ) { ?>
          <?php  foreach ( $terms as $term ) { ?>
                  <section class="section-cat">
                    <h2>
                            <?php echo $term->name; ?>
                    </h2>
    
    <ul>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php wc_get_template_part( 'content', 'product' ); ?>
    
    <?php endwhile; ?>
    </ul>
    </section>
    <?php  } ?>
    <?php  } ?>

    but it shows all the products under each subcategory

    i cant figure it out

    any help please ?

    https://wordpress.org/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You are querying all the products in each sub-category section. You need to have a more specific query on line 14 that only looks for products with that term.

    I believe this plugin may do what you are looking for though: https://woocommerce.com/products/woocommerce-nested-category-layout/

    Thread Starter leoskelter

    (@leoskelter)

    the working example…

    <?php
    $parentid = get_queried_object_id();
    $args = array( 'parent' => $parentid );
    $terms = get_terms( 'product_cat', $args );?>
    <div class="product-cats-arch">
    	 <?php woocommerce_breadcrumb(); ?>
    <?php if ( $terms ) { ?>
          <?php  foreach ( $terms as $term ) { ?>
                  <section class="section-cat">
                    <h2>
                            <?php echo $term->name; ?>
                    </h2>
    
    <ul>
    <?php
      $query_args = array( 'post__not_in' => array( get_the_ID() ), 'posts_per_page' => -1, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
        array(
          'taxonomy' => 'product_cat',
          'field' => 'id',
          'terms' => $term->term_id,
    
        )));
    $wooprods = get_posts($query_args);
    
    foreach ($wooprods as $dprod) {
    
    $product = get_product($dprod->ID);
    ?>
    <li class="product type-product">
    	<a>" class="woocommerce-LoopProduct-link">
    		<?php
    			echo $product->get_image('shop_catalog');
    		?>
    		<h3><?php echo $dprod->post_title; ?></h3>
    	</a>
    <?php
    	echo do_shortcode('[add_to_cart id="'.$dprod->ID.'"]');
    ?>
    
    <?php
    }
    ?>
    </ul>
    </section>
    <?php  } ?>
    <?php  } ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘archive page’ is closed to new replies.