简体   繁体   English

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

[英]Get all the product variations for specific product attributes values in Woocommerce

In WooCommerce *(latest version)* I have one variable product with Id: 9`.在 WooCommerce *(latest version)* I have one Id: 9` 的可变product with
With the attributes for variation below I created multiple product variations.通过下面的变体属性,我创建了多个产品变体。

Then I wanna get an specific product variation, from the parent product id ( Id: 9 ) and the following attribute values:然后我想从父产品 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

Below you have an screenshot of that variation:下面是该变体的屏幕截图:

在此处输入图片说明

I have tried the following codes with their corresponding results.我已经尝试了以下代码及其相应的结果。 For simplicity, for now, just tried with the pa_frame attribute only.为简单起见,现在只尝试使用pa_frame属性。

Try 1:尝试 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

Try 2:尝试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

Any idea on how to return all the variations with specific attribute values?关于如何返回具有特定属性值的所有变体的任何想法?

Product attributes in variable products are set as meta data in wp_postmeta database table.可变产品中的产品属性设置为wp_postmeta 数据库表中的元数据 Then you will need to use a Meta query instead of a Tax query.然后您将需要使用 Meta 查询而不是 Tax 查询。 Try this:尝试这个:

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

This should works as expected now…现在应该可以按预期工作了……

For your multiple product attributes pairs ( key / value ) as listed in your question you will just use them in your WP_Query this way:对于您的问题中列出的多个产品属性对(键/值),您只需以这种方式在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;
}

Then you will get normally the corresponding product variation (only one)…然后你通常会得到相应的产品变体(只有一个)......

Note: product attributes meta keys start all with attribute_pa_ instead of just pa_注意:产品属性元键全部以attribute_pa_开头,而不仅仅是pa_

Documentation: WP_Query and Custom Field Parameters (meta query)文档: WP_Query 和自定义字段参数(元查询)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM