首页 文章

产品变体的属性值

提问于
浏览
7

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

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

谢谢你的帮助!

6 回答

  • 1

    实际上,产品属性实际上是自定义分类中的术语,因此您只需要获取该特定分类中的术语 . 所有属性分类都以'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 );
    
  • 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);
    
  • 4

    我只想发布一个变体属性而不是所有属性;贡献此代码,以防它对其他人有帮助 .

    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 );
    }
    
  • 12

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

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

    希望这有助于某人 .

  • 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)
    
  • 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'];
      }
     }
    

相关问题