产品变体的属性值
我正面临着产品变体及其在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...
哪有我以正确的格式获取属性以显示?
感谢您的帮助!
实际上,产品属性实际上是自定义分类法中的术语,所以您只需要获取特定分类法中的术语。所有属性分类标准都以'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);
我做到这一点是通过使用 “get_post_meta” 的方式:
echo get_post_meta($variation_id, 'attribute_name_field', true);
希望这可以帮助别人。
不幸的是,它给出了小写版本。 – piersb 2016-02-11 14:18:37
我只想发布一个变体属性而不是所有的变体属性;如果它对其他人有帮助,则提供此代码。
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);
}
我以前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)
这似乎为我工作。希望这可以帮助。
$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);
谢谢你的回答,我试图通过获取条款来获得属性,但是我面临的问题是他们错误的格式化。这是全部小写字母和特殊字符被替换。 – 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