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;
感謝您回答這個@LoicTheAztec。這非常有幫助!我確實考慮過自定義字段的路由,但是我不想爲插件添加額外的依賴關係,因爲它將在許多站點中使用。 –