source

Woocommerce의 특정 제품 속성 값에 대한 모든 제품 변형 가져오기

nicesource 2023. 9. 21. 20:35
반응형

Woocommerce의 특정 제품 속성 값에 대한 모든 제품 변형 가져오기

In WooCommerce*(latest version)* I have one변수product withID: 9'.
아래의 변형에 대한 속성을 가지고 여러 가지 제품 변형을 만들었습니다.

그럼 모제품 id에서 특정 제품 변형을 받고 싶습니다 (Id: 9) 및 다음 속성 값:

<attribute_for_variation>: <attribute_value_to_filter>
pa_size: size_8x10
pa_material: mat_luster_photo_paper
pa_frame: fra_silver_wood
pa_mat_usage: musa_yes

아래에는 해당 변화에 대한 스크린샷이 있습니다.

enter image description here

저는 그에 상응하는 결과를 가지고 아래 코드들을 시도해 보았습니다.단순화를 위해, 지금은, 단지,pa_frame속성만 지정합니다.

시도 1:

static function filterVariations() {
    $query = [
        'post_parent' => 9,
        'post_status' => 'publish',
        'post_type' => ['product_variation'],
        'posts_per_page' => -1,
    ];
    $result = [];
    $wc_query = new \WP_Query($query);
    while ($wc_query->have_posts()) {
        $wc_query->next_post();
        $result[] = $wc_query->post;
    }
    return $result;
}
// ---> RESULT: all the variations, that's OK

시도 2:

static function filterVariations() {
    $query = [
        'post_parent' => 9,
        'post_status' => 'publish',
        'post_type' => ['product_variation'],
        'posts_per_page' => -1,
        'tax_query' => [
            'relation' => 'AND',
            [
                'taxonomy' => 'pa_frame',
                'field' => 'slug',
                'terms' => [ 'fra_silver_wood' ],
            ],
        ],
    ];
    $result = [];
    $wc_query = new \WP_Query($query);
    while ($wc_query->have_posts()) {
        $wc_query->next_post();
        $result[] = $wc_query->post;
    }
    return $result;
}
// ---> RESULT: empty list

특정 속성 값을 가진 모든 변형을 반환하려면 어떻게 해야 합니까?

가변제품의 제품속성은 wp_postmeta 데이터베이스 테이블에서 메타데이터로 설정됩니다.그러면 Tax 쿼리 대신 Meta 쿼리를 사용해야 합니다.시도해 보기:

static function filterVariations() {
    $query = new \WP_Query( array(
        'post_parent' => 9,
        'post_status' => 'publish',
        'post_type' => 'product_variation',
        'posts_per_page' => -1,
        'meta_query' => array( array(
            'key' => 'attribute_pa_frame',
            'value' => 'fra_silver_wood',
        ) ),
    ) );
    $result = array();
    if($query->have_posts()){
        while ($query->have_posts()) {
            $query->next_post();
            $result[] = $query->post;
        }
        wp_reset_postdata();
    }
    wp_reset_query();

    return $result;
}

이는 지금 예상대로 작동할 것입니다.

질문에 나열된 여러 제품 속성 쌍(키/값)의 경우에는 해당 속성을 사용합니다.WP_Query이런 식으로:

public function filterVariations() {
    $query = new \WP_Query( array(
        'post_parent' => 40,
        'post_status' => 'publish',
        'post_type' => 'product_variation',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key'   => 'attribute_pa_size',
                'value' => 'size_8x10',
            ),
            array(
                'key'   => 'attribute_pa_material',
                'value' => 'mat_luster_photo_paper',
            ),
            array(
                'key'   => 'attribute_pa_frame',
                'value' => 'fra_silver_wood',
            ),
            array(
                'key'   => 'attribute_pa_mat_usage',
                'value' => 'musa_yes',
            ),
        ),
    ) );
    $result = array();
    if($query->have_posts()){
        while ($query->have_posts()) {
            $query->next_post();
            $result[] = $query->post;
        }
        wp_reset_postdata();
    }
    wp_reset_query();

    return $result;
}

그러면 일반적으로 해당 제품의 변형(단 하나)을 얻게 됩니다.

참고: 제품 속성 메타 키는 모두 로 시작합니다.attribute_pa_만이 아니라pa_

설명서: WP_Query 사용자 정의 필드 매개 변수(메타 쿼리)

언급URL : https://stackoverflow.com/questions/50122976/get-all-the-product-variations-for-specific-product-attributes-values-in-woocomm

반응형