2014-11-23 47 views
6

我正面臨着產品變體及其在woocommerce中的屬性的大問題。我試圖顯示每個可用產品變體的每個屬性的表格。但Woocommerce以小寫形式保存後期元完成的屬性,替換斜槓和德國特殊字符,如ü,ö,ä等。我使用$ variation-> get_variation_attributes()獲取屬性。我搜索的數據庫中保存的值,可以在管理面板的下拉例如見,但它們都保存這樣不會對變化的鏈接,他們被分配到:產品變體的屬性值

a:5:{s:10:"bestell-nr";a:6:{s:4:"name";s:11:"Bestell-Nr.";s:5:"value";s:9:"1 | 2 | and so on... 

哪有我以正確的格式獲取屬性以顯示?

感謝您的幫助!

回答

10

實際上,產品屬性實際上是自定義分類法中的術語,所以您只需要獲取特定分類法中的術語。所有屬性分類標準都以'pa_'開頭。所以size屬性是一個'pa_size'分類。變體ID是變體的帖子ID。

但是這取決於你想如何顯示,WooCommerce具有用於顯示變化的所有屬性的內置功能:

以下將顯示所有一個變化的定義屬性的列表。

echo wc_get_formatted_variation($product->get_variation_attributes()); 

和傳球true第二個參數將顯示一個平面列表:

echo wc_get_formatted_variation($product->get_variation_attributes(), true); 
+0

謝謝你的回答,我試圖通過獲取條款來獲得屬性,但是我面臨的問題是他們錯誤的格式化。這是全部小寫字母和特殊字符被替換。 – user3357332 2014-11-24 00:01:45

+0

如果你知道分類標識,你也可以['get_the_terms()'](http://codex.wordpress.org/Function_Reference/get_the_terms)。你可以編輯你的問題來解釋你需要什麼格式/爲什麼'wc_get_formatted_variation()'不適合你? – helgatheviking 2014-11-24 00:24:35

+0

感謝@helgatheviking片段,我不知道wc_get_formatted_variation()func' – Dan 2015-09-18 11:47:50

1

我做到這一點是通過使用 「get_post_meta」 的方式:

echo get_post_meta($variation_id, 'attribute_name_field', true); 

希望這可以幫助別人。

+0

不幸的是,它給出了小寫版本。 – piersb 2016-02-11 14:18:37

3

我只想發佈一個變體屬性而不是所有的變體屬性;如果它對其他人有幫助,則提供此代碼。

get_variation_attributes()獲取所有屬性

wc_get_formatted_variation()返回數組它遞給

$attributes = $productVariation->get_variation_attributes() ; 
if ($attributes [ 'attribute_pa_colour' ]) { 
    $colour = [ 'attribute_pa_colour' => $attributes [ 'attribute_pa_colour'] ]; 
    echo wc_get_formatted_variation ($colour); 
} 
0

我以前wp_get_post_terms獲得正確的方差屬性的格式化版本。

global $product; 
    $variations = $product->get_available_variations(); 
    $var = []; 
    foreach ($variations as $variation) { 
     $var[] = $variation['attributes']; 
    } 
    var_dump($var); 
    //xxx to get attribute values with correct lower-upper-mixed-case 
    foreach ($var as $key => $arr) { 
     foreach ($arr as $orig_code => $lowercase_value) { 
     $terms_arr = wp_get_post_terms($product->id, str_replace('attribute_','',$orig_code), array('fields' => 'names')); 
     foreach ($terms_arr as $term) { 
      if (strtolower($term) == $lowercase_value) { 
       $var[$key][$orig_code] = $term; 
       break; 
      } 
     } 
     } 
    } 
    var_dump($var); 

結果:

之前硬編碼

array (size=1) 
    0 => 
    array (size=2) 
     'attribute_pa_width' => string 'none' (length=4) 
     'attribute_pa_code' => string 'valancese' (length=9) 

硬代碼後:

array (size=1) 
    0 => 
    array (size=2) 
     'attribute_pa_width' => string 'None' (length=4) 
     'attribute_pa_code' => string 'ValanceSe' (length=9) 
3

這似乎爲我工作。希望這可以幫助。

$post = get_post(); 
$id = $post->ID; 
$product_variations = new WC_Product_Variable($id); 
$product_variations = $product_variations->get_available_variations(); 
print_r($product_variations); 

這可以在class-wc-product-variable中找到。php

所以基本上如果你在那個頁面上看看,你可以找到一堆有用的功能。

這是我放在一起的東西。

$product_children = $product_variations->get_children(); 

$child_variations = array(); 

foreach ($product_children as $child){ 

$child_variations[] = $product_variations->get_available_variation($child); 

} 

print_r($child_variations);