由于订单商品的 WooCommerce 3.0+ 有新的对象类 WC_Order_Item_Product。
现在无法像以前一样直接访问订单商品属性
因此,如果您查看输出的原始数据,您会发现每个订单项现在都是一个对象,并且您将能够以独占方式访问受保护的数据:
-
WC_Order_Item_Product getters 方法(或用 setters 方法改变它)...
-
WC_Order_Item get_formatted_meta_data( '', true ) 方法来访问所有元数据。它提供了一组可访问对象。请参阅WC_Data 方法get_meta() 以访问每个元数据。
-
WC_Data getters 方法取消保护此数据并使用以下方法通过数组访问它:
-
get_data() (这个方法很有用)
-
get_meta() (这个方法最有用)
get_data_keys()
-
get_meta_data() (不取消保护数据,使用get_formatted_meta_data())
-
wc_get_order_item_meta() 专用功能。
WC_Order_Item_Product getters 方法:
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo $item->get_type().'<br>'; // The order item type
echo $item->get_product_id().'<br>'; // The Product ID
echo $item->get_variation_id().'<br>'; // The variation ID
echo $item->get_quantity().'<br>'; // Line item quantity
echo $item->get_subtotal().'<br>'; // Line item subtotal
echo $item->get_total().'<br>'; // Line item total
// The associated product object (which properties can't be accessed directly too)
echo '<pre>'; print_r( $item->get_product() ); echo '</pre>';
// ... and so on ...
## Testing raw output (protected)
// echo '<pre>'; print_r($item); echo '</pre>';
}
wc_get_order_item_meta() 函数。在这里,您可以进入 wp_woocommerce_order_itemmeta 表并使用相应的 meta_key 输出项目 ID 的任何数据(对于 line_item 数据类型项目 ID): p>
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo wc_get_order_item_meta( $item_id, '_product_id', true). '<br>'; // Product ID
echo wc_get_order_item_meta( $item_id, '_variation_id', true). '<br>'; // Variation ID
echo wc_get_order_item_meta( $item_id, '_qty', true). '<br>'; // quantity
echo wc_get_order_item_meta( $item_id, '_line_subtotal', true). '<br>'; // Line subtotal
// ... and so on ...
## Testing raw output (protected data)
// echo '<pre>'; print_r($item); echo '</pre>';
}
WC_Data 方法get_data() 方法(取消保护数组中的数据):
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
// Get the most useful Item product data in an accessible array
$item_data = $item->get_data();
echo $item_data['id'].'<br>'; // The order item ID
echo $item_data['order_id'].'<br>'; // The order ID
echo $item_data['product_id'].'<br>'; // The Product ID
echo $item_data['variation_id'].'<br>'; // The Variation ID
echo $item_data['name'].'<br>'; // The Product title (name)
echo $item_data['quantity'].'<br>'; // Line item quantity
echo $item_data['subtotal'].'<br>'; // Line item subtotal
echo $item_data['total'].'<br>'; // Line item total
// ... and so on ...
WC_Data 方法 get_meta() 方法(通过元键访问每个属性):
// Get an instance of the WC_Order object
$order = wc_get_order(147);
// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
echo $item->get_meta('_product_id').'<br>'; // The Product ID
echo $item->get_meta('_variation_id').'<br>'; // The Variation ID
echo $item->get_meta('_qty').'<br>'; // Line item quantity
echo $item->get_meta('_line_subtotal').'<br>'; // Line item subtotal
echo $item->get_meta('_line_subtotal_tax').'<br>'; // Line item subtotal tax
echo $item->get_meta('_line_total').'<br>'; // Line item total
echo $item->get_meta('_line_tax').'<br>'; // Line item total tax
// Product attributes for variation
echo $item->get_meta('pa_color').'<br>'; // Color
echo $item->get_meta('pa_size').'<br>'; // Color
// Custom item meta gata
echo $item->get_meta('custom_meta_key').'<br>'; // custom meta key visible
// ... and so on ...
相关:How to get WooCommerce order details