wordpress - WooCommerce:从匹配属性中获取产品变体 ID
问题描述
我如何从自定义产品循环中获取产品变体 ID。我有变化属性,例如,
{ 'pa_color'=>'red','pa_size'=>'large'}
解决方案
Set of attributes to match are
[
'attribute_pa_color' => 'blue',
'attribute_pa_size' => 'small',
];
Below is the function I ended up creating to achieve this:
/**
* 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
);
}
推荐阅读
- azure - Azure 监控:MetricsAlert 与 AlertRules,在 Monitor 中看不到 AlertRules
- python - I'm trying to make an API in python that receives data in JSON
- react-admin - Customize List as Cards
- sql - append the query with custom data modifications to add more
- php - How to use GROUP BY method in php-mysql
- javascript - Cannot Add Option List to Multiple Select jQuery
- javascript - 如何在 Angular 应用程序中包含 Telegram 登录小部件?
- user-interface - How to set correct sizes from Adobe Illustrator / Sketch to flutter?
- javascript - 内容编辑后如何更改单元格背景?
- python - 如何在鼠标单击时在 FigureCanvasQTAgg 上画一个圆圈