【发布时间】:2017-09-22 21:19:56
【问题描述】:
我正在尝试获取 2 个特定自定义产品属性的值,但是我无法返回任何值。
以下是我的代码的精简版本,因为很多内容与此问题无关。
在我的产品中,我设置了一个名为 carrier 和值 AA12345 的自定义属性。我还将属性template 设置为BB67890。我真的不确定我在这里做错了什么。
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;
【问题讨论】:
标签: php wordpress woocommerce attributes product