bst*_*s21 6 php wordpress class orders woocommerce
我正在尝试获取订单的订单项.
我这样做:
$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
print_r('<pre>----');
print_r($lineItem);
print_r('----</pre>');
}
Run Code Online (Sandbox Code Playgroud)
我可以看到我需要的所有数据,但数组显示:
[meta_data:protected] => Array
Run Code Online (Sandbox Code Playgroud)
如何访问此数组以获取值?
谢谢.
Loi*_*tec 22
由于WooCommerce 3.0+ for Order项目有新的Object类
WC_Order_Item_Product.
现在无法像以前那样直接访问订单商品属性
因此,如果您查看输出原始数据,您将看到每个行项目现在都是一个对象,您将能够使用以下方式访问受保护的数据:
WC_Order_Item_Product getters方法(或用setter方法改变它)......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
// 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>';
}
Run Code Online (Sandbox Code Playgroud)
该wc_get_order_item_meta()功能.在这里,您可以wp_woocommerce_order_itemmeta使用相应的meta_key(对于line_item数据类型项ID)进入表并输出项ID的任何数据:
// 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>';
}
Run Code Online (Sandbox Code Playgroud)
该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 ...
Run Code Online (Sandbox Code Playgroud)
该WC_Data方法get_meta()的方法(来访问它的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
echo $item->get_meta('_custom_meta_key').'<br>'; // custom meta key not visible
// ... and so on ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13863 次 |
| 最近记录: |