我需要回显折扣在20%-40%之间的产品。我尝试使用元查询,但它并没有比较愤怒的百分比。我的代码:
<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>
获取折扣百分比的我的代码:
global $product;
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );