
Wani
Estoy tratando de obtener productos en venta en una consulta personalizada, cualquiera puede ayudar a formar la consulta o la condición para que suceda…
$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;
aquí está el trabajo en el que estoy trabajando

Nikunj Katrotiya
global $product;
if ( $product->is_on_sale() ) {
do_something();
}
Tengo dos tipos de código para hacer esto.
<!-- 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 -->
Y el segundo es el siguiente: antes de obtener este código, revise esto Enlace
<!-- 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 -->
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
)
),
);
$query = new WP_Query($args);
if ($query->have_posts()) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ($query->have_posts()) : $query->the_post();
echo 'on sale';
endwhile;
echo '</ul>';
print_r($products_onsale);
endif;
Obtendrá solo productos con un precio de venta superior a cero (onsale)
Puede usar lo siguiente para verificar si el producto tiene precio de venta:
$sale_price = get_post_meta( $product_id, '_sale_price', true);
Si $sale_price es mayor que 0 y no está vacío, el producto está en oferta.
¡Espero que esto ayude!