Get a List of Products on Sale in WooCommerce
Check if the product is on sale
We can simply check if a product is on sale by using is_on_sale()
function.
<?php
global $product;
if ( $product->is_on_sale() ) {
// wow product is on sale
// do something
}
Get Products on Sale from All Categories
On contrary, getting a list of products that are on sale is a bit complex than check for a single product. We will use WP_QUERY
class. In addition, we will use wc_get_product_ids_on_sale()
function to get all the ids.
<?php
//arguments
$args = array(
'posts_per_page' => 24,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() )
);
//get products on sale using wp_query class
$products = new WP_Query( $args );
<?php if($products->have_posts()) : ?>
<?php while($products->have_posts()) : $products->the_post(); ?>
<?php the_title() ?>
<?php echo $product->get_price(); ?>
<?php echo $product->get_sale_price(); ?>
<?php endwhile; ?>
<?php else : ?>
<p> Sorry!No products on sale</p>
<?php endif; ?>
The above query will retrieve all the products that are on sale from all category.
Get Products on Sale from Specific Categories
We can also retrieve list of products from specific categories. For this we should pass ids of categories as an array.
<?php
$categories = array(1, 4, 7); //ids of categories
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => 24,
'orderby' => 'ASC',
'meta_query' => WC()->query->get_meta_query(),
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ),
);
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $categories,
'operator' => 'IN',
));
$products = new WP_Query( $query_args );
<?php if($products->have_posts()) : ?>
<?php while($products->have_posts()) : $products->the_post(); ?>
<?php the_title() ?>
<?php echo $product->get_price(); ?>
<?php echo $product->get_sale_price(); ?>
<?php endwhile; ?>
<?php else : ?>
<p> Sorry!No products on sale</p>
<?php endif; ?>
Author: NghiaMP
Its like you read my mind You appear to know so much about this like you wrote the book in it or something I think that you can do with a few pics to drive the message home a little bit but instead of that this is excellent blog A fantastic read Ill certainly be back
Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas
I have been browsing online more than three hours today yet I never found any interesting article like yours It is pretty worth enough for me In my view if all website owners and bloggers made good content as you did the internet will be a lot more useful than ever before
Wonderful web site Lots of useful info here Im sending it to a few friends ans additionally sharing in delicious And obviously thanks to your effort
Thank you for the auspicious writeup It in fact was a amusement account it Look advanced to more added agreeable from you By the way how could we communicate
Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas
helloI really like your writing so a lot share we keep up a correspondence extra approximately your post on AOL I need an expert in this house to unravel my problem May be that is you Taking a look ahead to see you
Thank you I have just been searching for information approximately this topic for a while and yours is the best I have found out so far However what in regards to the bottom line Are you certain concerning the supply
you are in reality a just right webmaster The site loading velocity is incredible It seems that you are doing any unique trick In addition The contents are masterwork you have performed a wonderful task on this topic
Attractive section of content I just stumbled upon your blog and in accession capital to assert that I get actually enjoyed account your blog posts Anyway I will be subscribing to your augment and even I achievement you access consistently fast
Hi there, just became alert to your blog through Google,
and found that it’s really informative. I’m gonna watch out for
brussels. I will appreciate if you continue this in future.
Many people will be benefited from your writing.
Cheers!
Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas
Hello Neat post Theres an issue together with your site in internet explorer would check this IE still is the marketplace chief and a large element of other folks will leave out your magnificent writing due to this problem
Simply wish to say your article is as amazing The clearness in your post is just nice and i could assume youre an expert on this subject Well with your permission let me to grab your feed to keep updated with forthcoming post Thanks a million and please carry on the gratifying work
I have been browsing online more than three hours today yet I never found any interesting article like yours It is pretty worth enough for me In my view if all website owners and bloggers made good content as you did the internet will be a lot more useful than ever before
I appreciated your work and the way you presented it here. The design is elegant and the topic is handled with style, but there is a certain harshness that you should address. If you do, I have no doubt that the same issues will continue to arise frequently unless you address this challenge.
Wow, your blog layout is incredible! How long have you been blogging? You make it seem so effortless. The design of your website is stunning, and the content is equally impressive.
BaddieHub Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.