在Woocommerce中获取自定义产品属性 [英] Get custom product attributes in Woocommerce
本文介绍了在Woocommerce中获取自定义产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Woocommerce中,我试图获取产品自定义属性值,但我失败了,我什么也没得到.
In Woocommerce, I am trying to get product custom attribute values but I fail miserably and I don't get anything.
所以我尝试了:
global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));
我正在获取这些原始数据:
And I'm getting this raw data:
[pa_koostis] => Array
(
[name] => pa_koostis
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
我知道有一个值,因为它显示在属性部分中,但是我无法找到一种方法来将其与我的自定义代码一起显示.
I know that there is a value because it is shown in the attribute section, but I just can't find a way to get it displayed with my custom code.
推荐答案
已编辑:
自Woocommerce版本3起已弃用
使用以下内容作为 @datafeedr 写道
Go with the following as @datafeedr wrote in his answer:
global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );
或更紧凑:
global $product;
$koostis = $product->get_attribute( 'pa_koostis' );
原始答案:
Original answer:
$result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));
这篇关于在Woocommerce中获取自定义产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文