Wordpress Woocommerce产品属性返回键不值 [英] Wordpress Woocommerce Product attribute return key not value

查看:81
本文介绍了Wordpress Woocommerce产品属性返回键不值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在woocommerce中获取大小属性的产品属性,这是我的代码:

I'm trying to get products attribute of size in woocommerce
and here's my code :

<?php
    $test = $_product->get_attributes();

    if ( $test != NULL ) {
        foreach($test['pa_size']['options'] as $size){
                if ($size !== NULL) {
                    echo apply_filters( 'woocommerce_cart_item_size',  $size , $cart_item, $cart_item_key );

                }   else  {
                    echo "Not Specified";
                }
        }
        } else {
        echo "Not Specified";
        }
?>

,这里如果产品具有属性,它将检查是否具有大小属性​​.
然后它将在var $ size
中返回此大小 我得到的结果不是属性值的问题.
值是(小,中或大)
但是我得到了这样的关键价值.

and here if the product has attributes it will check if has attribute of size.
then It will return this size in the var $size
the problem that I got the result not as the value of the attribute.
the value is ( small , medium or large )
but I got a key value like this.

48

所以我如何获取属性的值而不是键.
预先感谢.

So how can I get the value of the attribute not the key.
Thanks in Advance.

推荐答案

好吧,我在这里找到了解决方法
https://developer.wordpress.org/reference/functions/get_the_terms/

Well I found the solve here
https://developer.wordpress.org/reference/functions/get_the_terms/

这是代码:

$test = $_product->get_attributes();
if(!empty($test)) {
    $terms = get_the_terms($product_id, "pa_size");
    foreach ( $terms as $term ) {
    echo "<option>" . $term->name . "</option>";
    }
} else {
    echo "Not Specified";
}

这篇关于Wordpress Woocommerce产品属性返回键不值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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