In WooCommerce*(latest version)* I have one
variableproduct with
Id: 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:
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?