Woocommerce: find_matching_product_variation return zero

490 Views Asked by At

I've this code to find the variation id of a variable products based on selected attributes.

      $match_attributes =  array(
        'pa_color' => 'grey',
        'pa_base' => 'circular',
        'pa_text' => 'no',
        'pa_font' => 'kaushan-script'
      );

      $data_store   = WC_Data_Store::load( 'product' );
      $variation_id = $data_store->find_matching_product_variation(
        new \WC_Product( $myProductId), $match_attributes
      );

The $match_attributes is correct but still receive zero $variation_id = 0, there something wrong? There is another way to get the variation id?

Tnx

1

There are 1 best solutions below

0
On

solved by myself but maybe someone needs to solve the same problem, I write down what have worked for me:

  foreach ($match_attributes] as $attribute) {
    $options['attribute_'.$attribute["slug"]] = $attribute["option"];
  }

I've manually inserted the "attribute_" prefix, and now all works as expected.