获取 Woocommerce 中特定产品属性值的所有产品变体

Dav*_*ith 6 php wordpress product variation woocommerce

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

下面是该变体的屏幕截图:

在此处输入图片说明

我已经尝试了以下代码及其相应的结果。为简单起见,目前仅尝试使用该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
Run Code Online (Sandbox Code Playgroud)

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

关于如何返回具有特定属性值的所有变体的任何想法?

Loi*_*tec 5

可变产品中的产品属性设置为wp_postmeta 数据库表中的元数据。然后您将需要使用 Meta 查询而不是 Tax 查询。尝试这个:

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

现在应该可以按预期工作了……

对于您的问题中列出的多个产品属性对(键/值),您只需按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;
}
Run Code Online (Sandbox Code Playgroud)

然后你通常会得到相应的产品变体(只有一个)......

注意:产品属性元键开始全部attribute_pa_而不是仅仅pa_

文档:WP_Query 和自定义字段参数(元查询)