WooCommerce get_attribute返回null

问题描述:

我想获得2个特定的自定义产品属性的值,但是我无法返回任何值。WooCommerce get_attribute返回null

以下是我的代码的修剪版本,因为很多内容与此问题无关。

在我的产品中,我设置了名称为carrier和值为AA12345的自定义属性。我还设置了值为BB67890的属性template。我真的不确定我在这里做错了什么。

foreach($order-> get_items() as $item_key => $item_values): 

    $item_id = $item_values->get_id(); 
    $item_name = $item_values->get_name(); 
    $item_type = $item_values->get_type(); 
    $product_id = $item_values->get_product_id(); // the Product id 
    $wc_product = $item_values->get_product(); // the WC_Product object 
    ## Access Order Items data properties (in an array of values) ## 
    $item_data = $item_values->get_data(); 
    $product_name = $item_data['name']; 
    $product_id = $item_data['product_id']; 
    $variation_id = $item_data['variation_id']; 
    $quantity = $item_data['quantity']; 
    $tax_class = $item_data['tax_class']; 
    $line_subtotal = $item_data['subtotal']; 
    $line_subtotal_tax = $item_data['subtotal_tax']; 
    $line_total = $item_data['total']; 
    $line_total_tax = $item_data['total_tax']; 
    $order_id = $item_data['order_id']; 

    if ($product_id == 37) { 
    } else if ($product_id == 50) { 
    // Get Poduct attributes 
    $p = wc_get_product($product_id); 
    $patt_carrier = $p->get_attribute('carrier'); 
    $patt_template = $p->get_attribute('template'); 
     $product_type = "PICKANDPACK"; 
     $postData['bundles'][] = [ 
      'type' => $product_type, 
      'items' => [ 
       'bom' => [ 
       [ 
        'type' => 'CARD', 
        'stockId' => $item_data['sku'], 
        [ 
         'type' => 'CARRIER', 
         'quantity' => '1', 
         'stockId' => $patt_carrier, 
         'metadata' => [ 
          'template' => $patt_template, 
         ] 
        ], 
        ], 
       ], 


     ]; 
    } 
endforeach; 

你的代码是主要工作,只是有2个小错误和冗余:

  • 缺少一个右]$postData阵列,
  • 要获得产品SKU,你应该使用$product->get_sku()而不是$item_data['sku'],
  • 到WC_Product对象已在$item_values->get_product()中可用。

获取产品属性值的部分是正确的。

所以你的工作(测试)的代码应该是:

foreach($order-> get_items() as $item_key => $item_values): 
    $product_id = $item_values->get_product_id(); // the product id 
    $product = $item_values->get_product(); // <== the WC_Product object 
    $product_sku = $product->get_sku(); // <== the product sku 

    if ($product_id == 37) { 
    } else if ($product_id == 50) { 

     // Get Poduct attributes (==> working) 
     $patt_carrier = $product->get_attribute('carrier'); 
     $patt_template = $product->get_attribute('template'); 

     $product_type = "PICKANDPACK"; 
     $postData['bundles'][] = [ 
      'type' => $product_type, 
      'items' => [ 
       'bom' => [ 
        [ 
         'type' => 'CARD', 
         'stockId' => $product_sku, // <== The sku 
         [ 
          'type' => 'CARRIER', 
          'quantity' => '1', 
          'stockId' => $patt_carrier, 
          'metadata' => [ 
           'template' => $patt_template, 
          ] 
         ], 
        ], 
       ], 
      ], // <== one was missing 
     ]; 
    } 
     // Testing the raw output 
     echo '<pre>'; print_r($postData); echo '</pre>'; 
endforeach; 

使用自定义字段(二选一)

另一种方式来做到这一点,应使用自定义而不是产品属性(如果您不需要它们显示在“附加信息”产品选项卡中)。

如何做到这一点: Add custom fields to WooComerce product setting pages in the shipping tab

+0

感谢您回答这个@LoicTheAztec。这非常有帮助!我确实考虑过自定义字段的路由,但是我不想为插件添加额外的依赖关系,因为它将在许多站点中使用。 –