我以前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)
謝謝你的回答,我試圖通過獲取條款來獲得屬性,但是我面臨的問題是他們錯誤的格式化。這是全部小寫字母和特殊字符被替換。 – user3357332 2014-11-24 00:01:45
如果你知道分類標識,你也可以['get_the_terms()'](http://codex.wordpress.org/Function_Reference/get_the_terms)。你可以編輯你的問題來解釋你需要什麼格式/爲什麼'wc_get_formatted_variation()'不適合你? – helgatheviking 2014-11-24 00:24:35
感謝@helgatheviking片段,我不知道wc_get_formatted_variation()func' – Dan 2015-09-18 11:47:50