Get woocommerce subcategory products

16,294

There are many ways to do this in Wordpress. You could do a custom query using the WP_Query object to get all of the products in that category, which would be the most flexible option, but there is an easier way.

Woocommerce provides shortcodes specifically for showing products in a specific category. The output would use the templates that are already built in to Woocommerce.

<?php echo do_shortcode('[product_category category="appliances"]');?>

This will give you the products under a specific category.

In your code, you might do something like this:

<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <div class="products">
            <?php echo do_shortcode('[product_category category="'.$wsc->slug.'"]');?>
        </div>
    </li>
<?php endforeach;?>
</ul>

I noticed in your question that you're outputting a list. It seems likely to me that you wouldn't want to actually output the products (detailed template) below each of the categories, but you might rather want to show the number of products or product titles in a sublist.

Here's how you would show the number of products:

count;?>

You would use this anywhere in the foreach loop you have above.

Here's how you would show a sublist of titles of products in the list element for each category:

<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
    if($subcategory_products->have_posts()):?>
    <ul class="subcat-products">
        <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
        <li>
            <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                <?php the_title(); ?>
            </a>
        </li>
        <?php endwhile;?>
    </ul>
<?php endif; wp_reset_query(); // Remember to reset ?>

And here's how that would look in your code above:

<ul class="wsubcategs">
<?php
$wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
    <li>
        <a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
        <?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
            if($subcategory_products->have_posts()):?>
        <ul class="subcat-products">
            <?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
            <li>
                <a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
                    <?php the_title(); ?>
                </a>
            </li>
            <?php endwhile;?>
        </ul>
    <?php endif; wp_reset_query(); // Remember to reset ?>
    </li>
<?php endforeach;?>
</ul>
Share:
16,294
user3615851
Author by

user3615851

Updated on June 04, 2022

Comments

  • user3615851
    user3615851 almost 2 years

    I am trying to get the woocommerce subcategories with products to show under the main categories.

    <ul class="wsubcategs">
    <?php
    $wsubargs = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $category->term_id,
    'taxonomy' => 'product_cat'
    );
    $wsubcats = get_categories($wsubargs);
    foreach ($wsubcats as $wsc):
    ?>
    <li><a href="<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>"><?php echo $wsc->name;?></a>
    
    </li>
    <?php
    endforeach;
    ?>
    
    </ul>
    

    So far this has gotten me the corrent subcategories under the correct main categories, but no products are being shown under them. Any advice would be appreciated.

    O.