2017-09-22 72 views
1

我想獲得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; 

回答

1

你的代碼是主要工作,只是有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。這非常有幫助!我確實考慮過自定義字段的路由,但是我不想爲插件添加額外的依賴關係,因爲它將在許多站點中使用。 –