WooCommerce:从匹配属性获取产品变体ID [英] WooCommerce: Get Product Variation ID from Matching Attributes

查看:186
本文介绍了WooCommerce:从匹配属性获取产品变体ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从自定义产品循环中获取产品版本ID. 我有类似的变化属性,

How I get product variation id from custom product loop. I have variation attribute like,

{ 'pa_color'=>'red','pa_size'=>'large'}

推荐答案

要匹配的属性集

[
    '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
    );
}

这篇关于WooCommerce:从匹配属性获取产品变体ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆