在 Woocommerce 中获取自定义产品属性 [英] Get custom product attributes in Woocommerce

查看:40
本文介绍了在 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_get_product_terms 已被弃用自 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屋!

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