code

WP_Query에서 WooCommerce 특집 제품 가져오기

starcafe 2023. 2. 27. 23:25
반응형

WP_Query에서 WooCommerce 특집 제품 가져오기

WooCommerce를 버전 3.0으로 업데이트했는데, 테마에 특집 제품을 표시할 수 없습니다. 잠시 검색해서 WC에서 _feature를 삭제하고 분류에 추가했습니다.하지만 나는 내 주제가 어떻게 특집 상품들을 얻는지 잘 모르겠다.

다음은 잘못된 제품의 코드입니다.

$meta_query   = WC()->query->get_meta_query();
    $meta_query[] = array(
        'key'   => '_featured',
        'value' => 'yes'
    );

    $args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => $products,
        'orderby'             => $orderby,
        'order'               => $order == 'asc' ? 'asc' : 'desc',
        'meta_query'          => $meta_query
    );

또한 DataBase의 주요 항목이 어디에 있는지 알고 있다면정말 감사합니다.

Woocommerce 3에서는 Tax Query를 대신 사용해야 합니다.특징 제품은 이제 다음에서 처리되기 때문입니다.product_visibility 커스텀 분류법:

// The tax query
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);

// The query
$query = new WP_Query( array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'tax_query'           => $tax_query // <===
) );

참고 자료:

기능을 사용하여 피처링 제품 ID 배열을 가져올 수 있지만 Tax 쿼리를 사용하여WP_Query 그럭저럭 괜찮고 올바른 방법대로...

관련:

잘 될 거야.

이것은 오래된 질문이지만 wc_get_featured_product_ids()도 사용할 수 있습니다.

$args = array(
    'post_type'           => 'product',
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'post__in'            => wc_get_featured_product_ids(),
);

$query = new WP_Query( $args );

방금 여기서 발견했어.도움이 됐으면 좋겠네요!

이제 wc_get_products를 feature가 true로 설정된 상태로 사용할 수 있습니다.https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query 를 참조해 주세요.

$args = array(
    'featured' => true,
);
$products = wc_get_products( $args );

카테고리별 특집 상품을 원하시는 분은 이쪽 = > https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/ 에서 확인하실 수 있습니다.

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 12,
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                ),
            ),
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();

언급URL : https://stackoverflow.com/questions/46246614/get-woocommerce-featured-products-in-a-wp-query

반응형