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
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.com/en-IN/register?ref=UM6SMJM3
Your article helped me a lot, is there any more related content? Thanks! https://www.binance.info/cs/register?ref=S5H7X3LP
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.info/es/register-person?ref=T7KCZASX
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me. https://www.gate.io/share/XwNAUwgM
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/join?ref=P9L9FQKY
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.info/en-IN/register?ref=UM6SMJM3
Your article helped me a lot, is there any more related content? Thanks!
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Your article helped me a lot, is there any more related content? Thanks!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.