Attributes 产品变体的属性值
在商业中,我面临着一个关于产品变化及其属性的大问题。我试图显示一个表,其中包含每个可用产品变体的每个属性。但是Woocommerce将属性以小写形式保存在post meta complete中,替换斜杠和德语特殊字符,如u、ö、ä等。我使用$variation->get_variation_attributes()获取属性。我已经在数据库中搜索了您可以在管理面板的下拉列表中看到的保存值,但是它们是这样保存的,没有指向它们所分配到的变体的链接:Attributes 产品变体的属性值,attributes,format,woocommerce,product,variation,Attributes,Format,Woocommerce,Product,Variation,在商业中,我面临着一个关于产品变化及其属性的大问题。我试图显示一个表,其中包含每个可用产品变体的每个属性。但是Woocommerce将属性以小写形式保存在post meta complete中,替换斜杠和德语特殊字符,如u、ö、ä等。我使用$variation->get_variation_attributes()获取属性。我已经在数据库中搜索了您可以在管理面板的下拉列表中看到的保存值,但是它们是这样保存的,没有指向它们所分配到的变体的链接: a:5:{s:10:"bestell-nr";a:6
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属性将是“pau size”分类法。变体ID是变体的post 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”:
希望这对某人有所帮助。我只想发布一个变体属性,而不是所有属性;提供此代码以防对其他人有帮助 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_术语来获得正确的方差属性
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);
刚刚经历了这个…这是我的解决方案。它处理多个属性和所有变体。你只需要知道属性slug
$items = $order->get_items();
foreach( $items as $item_id => $product )
{
$ProductName = $product->get_name(); /
if($product->is_type( 'simple' ))
$CoreProductName = $ProductName; // No variance
else
{
list($CoreProductName, $ThrowAway) = explode(" - ",$ProductName, 2);
$variation_id = $product['variation_id'];
$variation = new WC_Product_Variation($variation_id);
$attributes = $variation->get_variation_attributes();
$SelectedAttribute1 = $attributes['attribute_YOUR_SLUG1_PER_PRODUCT'];
$SelectedAttribute2 = $attributes['attribute_YOUR_SLUG2_PER_PRODUCT'];
}
}
谢谢你的回答,我试着用术语来获取属性,但是我面临的问题是它们的格式错误。所有的字符都是小写的,特殊字符被替换。如果你知道分类法的ID,你也可以这样做。你能编辑你的问题来解释你需要什么格式的变量吗?/为什么
wc\u get\u formatted\u variation()
不适合你?感谢@helgatheviking的代码片段,我不知道wc\u get\u formatted\u variation()函数,不幸的是它给出了小写版本。
$items = $order->get_items();
foreach( $items as $item_id => $product )
{
$ProductName = $product->get_name(); /
if($product->is_type( 'simple' ))
$CoreProductName = $ProductName; // No variance
else
{
list($CoreProductName, $ThrowAway) = explode(" - ",$ProductName, 2);
$variation_id = $product['variation_id'];
$variation = new WC_Product_Variation($variation_id);
$attributes = $variation->get_variation_attributes();
$SelectedAttribute1 = $attributes['attribute_YOUR_SLUG1_PER_PRODUCT'];
$SelectedAttribute2 = $attributes['attribute_YOUR_SLUG2_PER_PRODUCT'];
}
}