Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 473
Wani Avatar asked Mar 14 '19 12:03

Wani


People also ask

How do I show only sale items in WooCommerce?

Go to Woocommerce Settings -> Products -> tab Display. Under Shop & Product Pages you will notice “Onsale Page”. Select page you created for on sale page. Save settings.

How do I get product SKU in WooCommerce?

Once you click on the edit option, you find all the available settings for that individual product. Scroll down and click on the inventory and there will be the option to set a WooCommerce product SKU. Here you can manually set the unique SKU for that product.

How do I put my website on sale on WordPress?

Sign in to WordPress. In the left-hand menu, click Products, which will expand and give additional options. Select Edit for the product to which you would like to add a sale price. In the Product Data section, on the General tab, enter a price into the Sale Price box.


2 Answers

global $product;
if ( $product->is_on_sale() )  {    
    do_something();
}
like image 179
Nikunj Kathrotiya Avatar answered Nov 09 '22 08:11

Nikunj Kathrotiya


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 -->
like image 44
Dhruv Avatar answered Nov 09 '22 07:11

Dhruv