iPascalCase
12/4/2017 - 3:45 PM

displaying the most sold products in woocommerce

place the below code whereever you like , to add more products per page change the number of posts_per_page to another number than 8 .

<!-- the most sold products -->
<div class="container">
    <div class="card-checkout">
        <div class="card-checkout">
            <h2 class="text-muted text-center">پر فروش ترین محصولات</h2>
        </div>
        <div class="row">
                <?php
                $args = array(
                    'post_type' => 'product',
                    'meta_key' => 'total_sales',
                    'orderby' => 'meta_value_num',
                    'posts_per_page' => 8,
                );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post();
                global $product;
                $currency = get_woocommerce_currency_symbol();
                $price = get_post_meta( get_the_ID(), '_regular_price', true);
                $sale = get_post_meta( get_the_ID(), '_sale_price', true); ?>
                <div id="custom-queries-mouse-hover" class="col-xs-3 col-md-3 col-lg-3">
                    <div class="card-profile card-plain">
                        <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>"title="<?php the_title(); ?>">

                            <?php if (has_post_thumbnail( $loop->post->ID )): ?>
                                <div class="col-lg-12">
                                    <?php echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); ?>
                                </div>
                            <?php else : echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="100%" height="100%" />'; ?>
                            <?php endif; ?>
                            <div class="col-lg-12">
                                <div class="content">
                                    <h6 class="card-title margin-top-30 text-center"><?php the_title(); ?></h6>
                                    <?php if($sale) : ?>
                                        <h6 class="category text-muted text-center"><del><?php echo $price; echo $currency; ?></del> <?php echo $sale; echo $currency; ?></h6>
                                    <?php elseif($price) : ?>
                                        <h6 class="category text-muted text-center"><?php echo $price; echo $currency; ?></h6>
                                    <?php endif; ?>
                                    <div class="footer">
                                        <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                                    </div>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="col-lg-12">
                        <div class="content">

                        </div>
                    </div>
                </div>
                <?php endwhile; ?>
            <?php wp_reset_query(); ?>
        </div>
    </div>
</div>
<!-- end of the most sold products -->