2017-08-05 79 views
0

我試圖讓大小的產品屬性,woocommerce
,這裏是我的代碼:
WordPress的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 

那麼我怎樣才能得到屬性的值不是關鍵。
在此先感謝。

回答

0

嗯,我發現這裏的解決
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"; 
} 
相關問題