【问题标题】:Wordpress Woocommerce Product attribute return key not valueWordpress Woocommerce 产品属性返回键不是值
【发布时间】:2018-01-13 04:08:50
【问题描述】:

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

<?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
中返回这个大小 我得到结果的问题不是属性的值。
值为(小、中或大)
但我得到了这样的关键值。

48

那么我怎样才能得到属性的值而不是键。
提前致谢。

【问题讨论】:

    标签: php wordpress woocommerce attributes


    【解决方案1】:

    我在这里找到了解决方案
    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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2017-04-07
      • 2019-02-12
      • 2013-10-31
      相关资源
      最近更新 更多