Wani
Wani

Reputation: 129

How to check if a product is onsale - WooCommerce

I'm trying to get onsale products in a custom query, anybody can help to form the query or the condition to make it happen...

$args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) :
        echo '<ul class="owl-carousel">';
        $products_onsale = array();
        while ( $query->have_posts() ) : $query->the_post();
            // get_template_part('template-parts/product-slider');
            $product_id = get_the_ID();
            $product = new WC_Product( $product_id );
            if ( $product->is_on_sale() ) {
                echo 'on sale';
            }
        endwhile;
        echo '</ul>';
        print_r( $products_onsale );
    endif;

here is the working i'm working on

Upvotes: 7

Views: 20891

Answers (6)

standard web iran
standard web iran

Reputation: 1

$args = array(
        'post_type'      => ['product','product_variation'],
        'posts_per_page' => 4,
        'meta_query'     => array(
            'relation' => 'OR',
            array(
                'key'           => '_sale_price',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
            )
        )
    );

Upvotes: 0

ahmad ashrafi
ahmad ashrafi

Reputation: 1

$args = array(
                    'post_type'      => 'product',
                    'posts_per_page' => -1,
                    'meta_query'     => array(
                        'relation' => 'AND',
                        array(
                            'key'     => '_stock_status',
                            'value'   => 'instock',
                            'compare' => '=',
                        ),
                        array(
                            'relation' => 'OR',
                            array(
                                'key'     => '_price',
                                'value'   => 0,
                                'compare' => '>',
                                'type'    => 'NUMERIC',
                            ),
                            array(
                                'key'     => '_price',
                                'compare' => 'NOT EXISTS',
                            ),
                        ),
                    ),
                );
    $query = new WP_Query($args);
if ($query->have_posts()) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ($query->have_posts()) : $query->the_post();

    echo 'on sale';

endwhile;
echo '</ul>';
print_r($products_onsale);
endif;

Upvotes: 0

Nikunj Kathrotiya
Nikunj Kathrotiya

Reputation: 963

global $product;
if ( $product->is_on_sale() )  {    
    do_something();
}

Upvotes: 26

Dhruv
Dhruv

Reputation: 612

I have two types of code for done this thing

  <!-- Get WooCommerce On-Sale Products fetch -->
        <ul class="get-onsale-product">
            <?php
                $args_for_onsale_product = array(
                    'post_type'      => 'product',
                    'posts_per_page' => 4, //If you want all the post replace 4 with -1.
                    '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'
                            )
                        )
                );
                $onsale_product_items = new WP_Query( $args_for_onsale_product );
                if ( $onsale_product_items->have_posts() ) {
                    while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
                        woocommerce_get_template_part( 'content', 'product' );
                    endwhile;
                } else {
                    echo __( 'Sorry We have no products.' );
                }
                wp_reset_postdata();
            ?>
        </ul>
        <!-- End WooCommerce On-Sale Products fetch -->

And second is following, Before you getting this code please review this link

 <!-- Get WooCommerce On-Sale Products fetch -->
            <ul class="get-onsale-product">
                <?php
                    $args_for_onsale_product = array(
                        'post_type'      => 'product',
                        'posts_per_page' => 4,
                        'post__in' => wc_get_product_ids_on_sale(),                        
                    );
                    $onsale_product_items = new WP_Query( $args_for_onsale_product );
                    if ( $onsale_product_items->have_posts() ) {
                        while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
                            woocommerce_get_template_part( 'content', 'product' );
                        endwhile;
                    } else {
                        echo __( 'Sorry We have no products.' );
                    }
                    wp_reset_postdata();
                ?>
            </ul>
            <!-- End WooCommerce On-Sale Products fetch -->

Upvotes: 2

Marshal Dudeja
Marshal Dudeja

Reputation: 76

You can use following to check if product has sale price:

$sale_price = get_post_meta( $product_id, '_sale_price', true);

If the $sale_price is greater than 0 and not empty, the product is on sale.

Hope this helps!

Upvotes: 1

mujuonly
mujuonly

Reputation: 11861

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => '_sale_price',
            'value' => 0,
            'compare' => '>'
        )
    ),
);
$query = new WP_Query($args);
if ($query->have_posts()) :
    echo '<ul class="owl-carousel">';
    $products_onsale = array();
    while ($query->have_posts()) : $query->the_post();

        echo 'on sale';

    endwhile;
    echo '</ul>';
    print_r($products_onsale);
    endif;

It will fetch only products with sale price greater than zero ( onsale )

Upvotes: 0

Related Questions