1
我试图让定制产品页面上的可变产品的变化。我有两个属性,一个是大小作为选择,另一个颜色色板。我不能显示我需要显示,当我使用下面的代码返回尺寸或颜色的文本名称不是大小或颜色色板选择下拉属性的问题。任何帮助吗?!获取Woocommerce变化属性
echo implode(', ', wc_get_product_terms($product_id, 'pa_colors'));
我试图让定制产品页面上的可变产品的变化。我有两个属性,一个是大小作为选择,另一个颜色色板。我不能显示我需要显示,当我使用下面的代码返回尺寸或颜色的文本名称不是大小或颜色色板选择下拉属性的问题。任何帮助吗?!获取Woocommerce变化属性
echo implode(', ', wc_get_product_terms($product_id, 'pa_colors'));
这是一个简短的代码来解决你的问题,我让你整个代码,你只能使用你需要的。
首先检查get_product
函数是否存在,并检查产品类型,以创建一个带有id的正确产品对象(在我的情况下为$idProduct
)。
它woocommerce 3.x的工作,我没有测试它woocommerce < 3.x的
if(function_exists('get_product')) {
$product = get_product($idProduct);
if ($product->is_type('variable')) {
$product = new WC_Product_Variable($idProduct);
$available_variations = $product->get_available_variations(); //get all child variations
$variation_variations = $product- >get_variation_attributes(); // get all attributes by variations
// taxonomy => terms
// pa_attribute-color => array('blue', 'red', green)
// Use ex: get_taxonomy('pa_attribute-color')->labels; to get the Name and not the slug to attributes, it can be the taxonomy
// Use ex: get_term_by('name', 'pa_attribute-color', 'pa_attribute-color); to get the Name/label
$result = array($available_variations , $attributes); // only to see the result you can use var_dump, error_log, etc.
//...
//...
}elseif ($product->is_type('bundle') && class_exists('WC_Product_Bundle')) {
$product = new WC_Product_Bundle($idProduct);
}else{
$product = new WC_Product($idProduct);
}
}
你也尝试:
$product->get_attribute($key);
wc_attribute_label($key);
其中$key
可以pa_color,pa_size等
我希望帮你。