6
votes

In WooCommerce*(latest version)* I have onevariableproduct withId: 9`.
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:

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

enter image description here

I have tried the following codes with their corresponding results. For simplicity, for now, just tried with the pa_frame attribute only.

Try 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:

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?

1

1 Answers

5
votes

Product attributes in variable products are set as meta data in wp_postmeta database table. Then you will need to use a Meta query instead of a Tax query. 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:

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_

Documentation: WP_Query and Custom Field Parameters (meta query)