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
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.
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.
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.
global $product;
if ( $product->is_on_sale() ) {
do_something();
}
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 -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With