1

I am trying to pick only OnSale products in a WooCommerce loop through a snippet. For that I tried to use below code but this one is picking all products. I only want to pick OnSale products in loop.

<!-- WooCommerce New Products -->
<ul class="products">
    <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 4
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul>
<!-- WooCommerce New Products -->

So for my desire, what should I do?

7
  • 1
    3rd party plugins and themes are off topic here. You should contact developer or ask in WooCommerce support forum on wordpress.org – Robert hue Nov 28 '14 at 17:03
  • @Roberthue Its really funny what you said here. :P You have time then search WooCommerce in this website and see how many question you are able to see here. If you are not able then open wordpress.stackexchange.com/search?q=WooCommerce and see there. – Muhammad Hassan Nov 29 '14 at 3:57
  • 2
    Woocommerce questions is off topic. We are busy closing all of these questions if you haven't noticed that. Your question will also be closed as off topic. Sorry about that. Thank you – Pieter Goosen Nov 29 '14 at 4:07
  • 1
    Please see these related threads on meta – Pieter Goosen Nov 29 '14 at 4:11
  • 1
    So you guys first close the WooCommerce tag so no will mention it again. First make yourself perfect then ask others for it. – Muhammad Hassan Nov 29 '14 at 5:28
5

Finally, I got the code. Using the code below, you can pick OnSale products that are either Simple Product On Sale or Variable Product On Sale. Keep in mind that this code will pick every post in which you added a sale price. So avoid adding the price in this field (Shown in attached Image) if your sale price is same as the original price because then it will not be a sale product but this code will pick it in sale products loops.

WooCommerce Product Data Form ScreenShot

<!-- WooCommerce On-Sale Products -->
<ul class="products">
    <?php
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 4,
            'meta_query'     => array(
                    'relation' => 'OR',
                    array( // Simple products type
                        'key'           => '_sale_price',
                        'value'         => 0,
                        'compare'       => '>',
                        'type'          => 'numeric'
                    ),
                    array( // Variable products type
                        'key'           => '_min_variation_sale_price',
                        'value'         => 0,
                        'compare'       => '>',
                        'type'          => 'numeric'
                    )
                )
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul>
<!-- WooCommerce On-Sale Products -->
2
  • Unfortunately this doesn't seem to work for me for variation products. The "_min_variation_sale_price" field is missing. WooCommerce probably changed it over the years. – Jordan Carter Mar 26 '18 at 17:30
  • 1
    The following worked for me. Add to query args: 'post__in' => wc_get_product_ids_on_sale(), – Jordan Carter Mar 26 '18 at 17:39

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