programing

판매중인 상품 확인 방법 - WooCommerce

css3 2023. 10. 7. 12:08

판매중인 상품 확인 방법 - 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;

여기 제가 하고 있는 작업이 있습니다.

global $product;
if ( $product->is_on_sale() )  {    
    do_something();
}

나는 이 일을 하기 위한 두가지 코드가 있습니다.

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

다음을 이용하여 제품에 판매가격이 있는지 확인할 수 있습니다.

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

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

판매가격이 0보다 큰 상품에 한해 제공됩니다(판매중)

언급URL : https://stackoverflow.com/questions/55162578/how-to-check-if-a-product-is-onsale-woocommerce