Woocommerce products from category loop

woocommerce display products by category
woocommerce query products by category
woocommerce category loop
woocommerce get products by category id
how to display particular category product in woocommerce
loop through all product category woocommerce
woocommerce category page
woocommerce get list of products in category

In Woocommerce, I Need some help with this custom product loop, in my code my result is : how it looks like

The loop doesn't stop and it is looping the same products for three or four times.

The code I am using is here :

<div class="container">
    <div id="default_products_page_container" class="wrap wpsc_container">
        <?php
            remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
            remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
            $cat = get_query_var( 'product_cat' );
            $args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $cat, 'orderby' => 'rand' );
            $loop = new WP_Query( $args );
        ?>
        <div class="wpsc_default_product_list">
            <?php
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                <div class="default_product_display product_view_<?php echo get_permalink( $loop->post->ID ); ?>group">
                    <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 if (has_post_thumbnail( $loop->post->ID ))
                    {            
                    ?>         
                    <div class="product-image-thumb"> 
                        <img src="<?php echo the_post_thumbnail_url( $loop->post->ID );?>"/>
                    </div>
                    <?php
                    } else 
                    {?>
                    <div class="product-image-thumb">
                        <a>
                            <img src="<?php echo woocommerce_placeholder_img_src();?>"/>
                        </a>
                    </div>
                    <?php } ?>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php wp_reset_query(); ?>
    </div>
</div>

Your code should work, although it will take all products in the loop (including those marked as "draft" when you are logged in). You can define only published by adding 'post_status' => 'publish' to your argument variable.

And you should take a look at those anchor tags (specifically the close part)

WooCommerce: products-of-category loop bizarre problems, Hard to tell from your images but i'm nearly 100% you are not closing the table tags correctly and what you are describing is exactly what would happen in this� Route empowers your brand with a premium post-purchase shopping experience with. real-time updates, visual order tracking, package protection, and effortless claim filing.

You need to do a tax query instead this way:

$loop = new WP_Query( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'tax_query' => array( array(
        'taxonomy'         => 'product_cat',
        'field'            => 'slug', // Or 'term_id' or 'name'
        'terms'            => get_query_var( 'product_cat' ), // A slug term
        // 'include_children' => false // or true (optional)
    )),
    'orderby' => 'rand'
) );

Tested and works on Woocommerce product category archive pages…

How to Display Product specific to a category with WooCommerce , You need to create a new loop for that. Here's the code I use for displaying products from a specific category on the home page: <ul class="products"> <? php� Awesome Support, Apps, and Design. Everything You Need To Start Selling Online.

I Hope this code gonna help you guys :)

    <ul class="products">
    <?php
    $args = array(
        'product_cat' => 'Shampoo',
        'posts_per_page' => 4,
        'orderby' => 'rand'
    );
    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post();
        global $product; ?>
        <div class="row">
            <!-- <h2>Shampoo</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>

                </a>

                <?php woocommerce_template_loop_add_to_cart($loop->post, $product); ?>
            </li>
        </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
</ul>
<!--/.products-->

Woocommerce: sorting products by their category in a foreach loop , [This thread is closed.] I'm doing a custom product-archive.php as my main shop page, so, in my child theme I have this custom product loop:… Save Time And Money With The #1 Cloud-Based Shipping Software. Trusted By 50,000+ Brands. Boost Your Bottom Line With Discounted Rates For Our 55+ Carriers. Save Up to 90%.

How to Display Categories in WooCommerce, Products; Categories (on shop page) or subcategories (on category are displayed in the HTML by running a loop through the child ID array. Start an online store in minutes and be selling in no time! Try it Free.

[Woocommerce] How to Display Products from Specific Category , [Woocommerce] How to Display Products from Specific Category class=" product">; <a href="<?php echo get_permalink( $loop->post->ID ) ? It’s actually fairly straightforward. First I get and set the variables for paging and order display in lines 7-11, then I get my Products, then I hack the WooCommerce loop variable with some parameters it needs in lines 25-30, then I finally loop through them using a foreach loop instead of the usual while loop because I don’t have the Loop methods to rely on as mentioned above.

Sample products loop, Sample products loop. This is a Developer level doc. If you are unfamiliar with code/templates and resolving potential conflicts, select a WooExpert or Developer� The eCommerce plugin Woocommerce is pretty standard for WordPress retail websites. Woocommerce by default does not display categories on the product loop (shop page), but I recently encountered an instance where we wanted to display all the categories that each product was in within the normal loop.

Comments
  • please edit the code for better indentation
  • Same problem. I have only 6 products in my shop, but the code was printing 9 :D
  • I think the main problem is at posts per page
  • @AurimasGelžinis The post_per_page limit the number of displayed products… If you use the value -1, there is no limitation… I have tested your code and for me it works without duplicated products… So there is something else somewhere that is making this happen.
  • It can be because of my css? when i do it without, it work's great