WooCommerce:从匹配属性中获取产品变体 ID
WooCommerce: Get Product Variation ID from Matching Attributes
我如何从自定义产品循环中获取产品变体 ID。
我有变体属性,例如
{ 'pa_color'=>'red','pa_size'=>'large'}
要匹配的属性集是
[
'attribute_pa_color' => 'blue',
'attribute_pa_size' => 'small',
];
下面是我最终创建的函数:
/**
* Find matching product variation
*
* @param $product_id
* @param $attributes
* @return int
*/
function find_matching_product_variation_id($product_id, $attributes)
{
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
new \WC_Product($product_id),
$attributes
);
}
这里是 find_matching_product_variation !
的完整示例代码
$product_id = 1; //Added Specific Product id
$match_attributes = array(
"attribute_pa_color" => 'blue',
"attribute_pa_size" => 'Large'
);
$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation(
new \WC_Product( $product_id),$match_attributes
);
我如何从自定义产品循环中获取产品变体 ID。 我有变体属性,例如
{ 'pa_color'=>'red','pa_size'=>'large'}
要匹配的属性集是
[
'attribute_pa_color' => 'blue',
'attribute_pa_size' => 'small',
];
下面是我最终创建的函数:
/**
* Find matching product variation
*
* @param $product_id
* @param $attributes
* @return int
*/
function find_matching_product_variation_id($product_id, $attributes)
{
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
new \WC_Product($product_id),
$attributes
);
}
这里是 find_matching_product_variation !
的完整示例代码$product_id = 1; //Added Specific Product id
$match_attributes = array(
"attribute_pa_color" => 'blue',
"attribute_pa_size" => 'Large'
);
$data_store = WC_Data_Store::load( 'product' );
$variation_id = $data_store->find_matching_product_variation(
new \WC_Product( $product_id),$match_attributes
);