Ecommerce Shopify WordPress Discussion

WooCommerce: Get Product Variation ID By Matching Attributes

I want to get the variation id with respect to the mataching attribues with simple & optimized code. function zx_get_variation( $product_id, $attributes ) { $CPT = new WC_Product_Data_Store_CPT(); $variation_id = $CPT->find_matching_product_variation( new WC_Product($product_id), $attributes ); return $variation_id; } This is returning "0" But There is a matching variation avaiable. Variation Screenshot Important: I do not want to use $product->get_available_variations() as I do not want to use nested loop. $products_object = []; $attributes = [ 'attribute_pa_length' => $filter_length, // term_slug 'attribute_pa_material' => $filter_type, // term_slug 'attribute_pa_eye-type' => $filter_material // term_slug ]; if( $products_2->have_posts() ): while( $products_2->have_posts() ): $products_2->the_post(); $zx_product_var = zx_get_variation( get_the_ID(), $attributes ); $product_ids[] = get_the_ID(); $products_object[] = [ 'product_id' => get_the_ID(), 'product_title' => get_the_title( get_the_ID() ), 'product_price' => get_post_meta( get_the_ID(), '_price', true ), 'product_tax' => get_the_terms( get_the_ID(), 'product_cat' )[0]->name, 'product_sku' => get_the_sku( get_the_ID() ), 'post_date' => get_the_date( 'Ymd', get_the_ID() ), 'product_thumbnail' => wp_get_attachment_url( get_post_thumbnail_id() ), 'product_link' => get_permalink(), 'attribute_pa_length' => $filter_length, 'attribute_pa_eye_type' => $filter_type, 'attribute_pa_material' => $filter_material, 'variation_id' => $zx_product_var, ]; endwhile; endif;
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.