I need to echo products which have a discount between 20% to 40%. I tried using meta query but it is not comparing percentage rage. My Code :
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'BETWEEN',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Simple products type
'key' => '_sale_price',
'value' => 1000,
'compare' => '<',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul>
My code to get Discount Percentage :
global $product;
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );