0
votes

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.

<ul class="products"> 
    <?php
        global $post, $product;
        $categ = $product - > get_categories();
        $categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ); 
    ?> 
    <?php
        global $product;
        $args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
        $loop = new WP_Query($args);
        while ($loop - > have_posts()): $loop - > the_post();
        global $product; 
    ?> 
        <li>
            <a href = "<?php echo get_permalink(); ?>"> 
                <?php
                    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>
            <div>
                <h3>
                    <a href="<?php echo get_permalink(); ?>"><?php the_title();?></a> 
                    <span> 
                        <?php
                            if ($price_html = $product - > get_price_html()) { 
                        ?> 
                            <span class = "price">
                                <?php echo $price_html; ?>
                            </span> 
                        <?php } ?>
                    </span> 
                </h3>
            </div> 
            <div> 
                <p> 
                    <?php
                        $excerpt = get_the_excerpt();
                        echo string_limit_words($excerpt, 15); 
                    ?> 
                </p>
            </div> 
        </li>
    <?php endwhile; ?>
</ul>

Screenshot

1

1 Answers

0
votes
 <?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(); ?>
                        <li>   
                            <a href="<?php echo get_permalink(); ?>">
                                <?php
                                    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>
                            <div>
                                <h3>
                                    <a href="<?php echo get_permalink(); ?>"><?php the_title();?></a>
                                        <span>
                                            <?php if ( $price_html = $product->get_price_html()) { ?>
                                                <span class="price"><?php echo $price_html; ?></span> 
                                            <?php } ?>
                                        </span>
                                 </h3>
                            </div>
                            <div>
                                <p>
                                    <?php
                                        $excerpt = get_the_excerpt();
                                        echo string_limit_words($excerpt,15);
                                    ?>
                                </p>
                            </div>
                        </li>
                <?php endwhile;?>
            </ul>
        </div>
        <?php woocommerce_product_loop_end(); ?>
    <?php endif; ?>