2
votes

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.

2

2 Answers

7
votes

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:

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>
0
votes
**Show Category and Sub Category in search Box**


----------


<?php
//template name:house

?>
<style>
.abc {
    margin-left: 10px;
}
</style>

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<select>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );

$abc=$cat->name;
if($abc=="") {
?>
<option value="<?php echo $cat->slug;?>"><?php echo $abc; ?></option>

<?php }
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
?>

<?php
if($sub_cats->$sub_category == 0)
{
$suv= $sub_category->cat_name;?>
<option class="abc" value="<?php echo $sub_category->slug; ?>"><span class="abc"><?php echo $suv; ?></span></option>




<?php } } } } } ?>

</select>