I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.

If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.

I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.

Thank you.

Finally found the solution myself, I'd like to thank everyone for helping me.

    <?php if ( have_posts() ) : ?>
    <?php woocommerce_product_loop_start(); ?>
    <?php woocommerce_product_subcategories(); ?>
        <div class="courses-main">
            <ul class="products">
                <?php while ( have_posts() ) : the_post(); ?>
                        <a href="<?php echo get_permalink(); ?>">
                                if ( has_post_thumbnail() ) {
                                    $image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
                                    $image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
                                    $image_link = get_permalink( $product_id );
                                    $attachment_count   = count( $product->get_gallery_attachment_ids() );
                                    echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s"  rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );
                                } else {
                                    echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
                                <a href="<?php echo get_permalink(); ?>"><?php the_title();?></a>
                                        <?php if ( $price_html = $product->get_price_html()) { ?>
                                            <span class="price"><?php echo $price_html; ?></span> 
                                        <?php } ?>
                                    $excerpt = get_the_excerpt();
                                    echo string_limit_words($excerpt,15);
            <?php endwhile;?>
    <?php woocommerce_product_loop_end(); ?>
<?php endif; ?>
  • you want to list all products from current category? and you have already fetched current category ? – codepixlabs Aug 30 '13 at 12:54
  • What is the reason you're doing this when the code is already written for you? If you're simply wanting to put the specific product category say in your main menu, you can already do that without any code... – user23654 Aug 30 '13 at 12:57
  • I've managed to get the current catergory posts but if one of the products are in multiple categories then it breaks. So I need to figue out how to list all products from within the current category and allow products within muliple categories. – Adam Wadsworth Aug 30 '13 at 12:57
  • I'm not sure I understand the question either. When you are on a particular category page WooCommerce will automatically display all the products in that category. How is what you are attempting different from a default category archive? – helgatheviking Aug 30 '13 at 13:03

You said you are able to get current product category name, store it in $current_product_category variable now try this code ..

<ul class="products">
        $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => $current_product_category, 'orderby' => 'rand' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <h2><?php echo $current_product_category ?></h2>

                <li class="product">    

                    <a href="<?php echo get_permalink( $loop->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 ); ?>

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>

                        <h3><?php the_title(); ?></h3>

                        <span class="price"><?php echo $product->get_price_html(); ?></span>                    


                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>


    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
  • This works but if i have a single product in Construction and Environment at the same time it brings all products from these categories. I'm needing it to get all products from Construction and if one of the products are within another category then continue displaying this product without bringing all the other products from the other categories. – Adam Wadsworth Aug 30 '13 at 13:07
  • you mean it should display products only which are unique to current category ?? – codepixlabs Aug 30 '13 at 13:09
  • Yeah, Even if the product is also within another category. – Adam Wadsworth Aug 30 '13 at 13:10
  • Maybe seeing what I'm tying to do may help, this is the URL I'm currently working on. goo.gl/TXZynC – Adam Wadsworth Aug 30 '13 at 13:17
  • docs.woothemes.com/wc-apidocs/class-WC_Product.html check here look for function called get_categories which returns all the associated category of product . then u can may check if product is only assigned to one category display it else skip ! – codepixlabs Aug 30 '13 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.