如果您使用 get_id()
方法,则您的商品 ID 在代码中为 15
。
获取产品 ID:
获取产品 ID 的正确方法是:get_product_id()
WC_Order_Item_Product
获取变体 ID:
获取变体 ID 的正确方法是:get_variation_id()
WC_Order_Item_Product
获取订单 ID
获取订单 ID 的正确方法是:get_order_id()
WC_Order_Item_Product
获取WC_Product对象
获取对象的正确方法是:get_product()
WC_Order_Item_Product
WC_Product
获取WC_Order对象
获取对象的正确方法是:get_order()
WC_Order_Item_Product
WC_order
使用WC_Data
方法获取和取消保护数据和元数据:
-
get_data()
get_meta_data()
从订单项 ID 中获取WC_Product
对象:
$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product();
$quantity = $item->get_quantity();
$order_id = $item->get_order_id();
$order = $item->get_order();
$item_id = $item->get_id();
$product_name = $item->get_name();
$sku = $product->get_sku();
$total = $item->get_subtotal();
$total_tax = $item->get_subtotal_tax();
$total = $item->get_total();
$total_tax = $item->get_total_tax();
从 WC_Order
对象获取订单项(并使用对象)::WC_product
$order_id = 156;
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item_id => $item ){
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product();
$quantity = $item->get_quantity();
$product_name = $item->get_name();
$sku = $product->get_sku();
$total = $item->get_subtotal();
$total_tax = $item->get_subtotal_tax();
$total = $item->get_total();
$total_tax = $item->get_total_tax();
}
###Accessing数据和自定义元数据:
取消保护WC_Order_Item_Product
数据和自定义元数据:
您可以使用所有WC_Order_Item_Product数据
方法,也可以使用以下WC_Data
方法取消对数据的保护:
$order_id = 156;
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item_id => $item ){
$item_product_data_array = $item->get_data();
$item_product_meta_data_array = $item->get_meta_data();
$meta_value = $item->get_meta( 'custom_meta_key', true );
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
$total = $item->get_subtotal();
$total_tax = $item->get_subtotal_tax();
$total = $item->get_total();
$total_tax = $item->get_total_tax();
}
阵列访问仍然可以(为了与旧阵列向后兼容)直接获取公共数据:
$order_id = 156;
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item_id => $item ){
$product_id = $item['product_id'];
$variation_id = $item['variation_id'];
$product_name = $item['name'];
$item_qty = $item['quantity'];
$line_total = $item['subtotal'];
$line_total_tax = $item['subtotal_tax'];
$line_total2 = $item['total'];
$line_total_tax2 = $item['total_tax'];
}
作为参考: