8

我在 woocommerce 中面临产品变体及其属性的大问题。我正在尝试显示每个可用产品变体的每个属性的表格。但是 Woocommerce 将 post meta 中的属性保存为小写,替换斜杠和德语特殊字符,如 ü、ö、ä 等。我使用 $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...

如何以正确的格式显示属性?

谢谢你的帮助!

4

6 回答 6

15

实际上,产品属性实际上是自定义分类法中的术语,因此您只需要获取该特定分类法中的术语。所有属性分类法都以“pa_”开头。因此,大小属性将是“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 );
于 2014-11-23T15:22:08.460 回答
5

这似乎对我有用。希望这可以帮助。

$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);
于 2017-01-27T13:55:16.227 回答
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 );
}
于 2016-02-11T14:51:19.827 回答
2

我这样做的方法是使用“get_post_meta”:

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

希望这可以帮助某人。

于 2015-08-13T14:05:21.047 回答
0

刚刚经历了这个......这是我的解决方案。它处理多个属性和所有变体。你只需要知道属性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'];
  }
 }
于 2019-03-30T20:16:28.530 回答
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)
于 2016-11-04T15:43:55.517 回答