У меня есть два типа кода для этой вещи
<!-- 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 -->
И второе следующее, прежде чем получить этот код, пожалуйста, просмотрите эту ссылку
<!-- 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 -->