简体   繁体   English

Wordpress Woocommerce产品属性返回键不值

[英]Wordpress Woocommerce Product attribute return key not value

I'm trying to get products attribute of size in woocommerce 我正在尝试在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";
        }
?>

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 然后它将在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. 提前致谢。

Well I found the solve here 我在这里找到了解决方法
https://developer.wordpress.org/reference/functions/get_the_terms/ https://developer.wordpress.org/reference/functions/get_the_terms/

and here's the code : 这是代码:

$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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM