如何检查产品是否在售 - WooCommerce

Wan*_*ani 4 php wordpress woocommerce

我正在尝试在自定义查询中获取特价商品,任何人都可以帮助形成查询或条件以使其发生...

$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;
Run Code Online (Sandbox Code Playgroud)

这是我正在做的工作

Nik*_*iya 18

global $product;
if ( $product->is_on_sale() )  {    
    do_something();
}
Run Code Online (Sandbox Code Playgroud)


Dhr*_*ruv 2

我有两种类型的代码来完成这件事

  <!-- 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 -->
Run Code Online (Sandbox Code Playgroud)

第二个是以下,在获得此代码之前,请查看此链接

 <!-- 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 -->
Run Code Online (Sandbox Code Playgroud)