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

时间:2019-03-14 12:29:41

标签: 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;

这是我正在从事的工作

4 个答案:

答案 0 :(得分: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 -->

第二步,在获取此代码之前,请先阅读此link

 <!-- 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 -->

答案 1 :(得分:0)

$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;

它将仅获取销售价格大于零(onsale)的产品

答案 2 :(得分:0)

您可以使用以下方法检查产品是否具有销售价格:

$sale_price = get_post_meta( $product_id, '_sale_price', true);

如果$ sale_price大于0并且不为空,则该产品正在销售中。

希望这会有所帮助!

答案 3 :(得分:0)

全局$ product;

如果($ product-> is_on_sale()) {

do_something();

}