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