访问订单项Woocommerce中的受保护数据3
问题描述:
我正在尝试获取订单的订单项。访问订单项Woocommerce中的受保护数据3
我这样做:
$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
print_r('<pre>----');
print_r($lineItem);
print_r('----</pre>');
}
我可以看到我需要的所有数据,但该阵列显示了这个:
[meta_data:protected] => Array
我如何可以访问此数组获取值?
谢谢。
答
由于WooCommerce 3.0+订单项有新的对象类
WC_Order_Item_Product
。
立即订购物品的属性可以,如果你看看你输出的原始数据,你会看到每个行项目现在是一个对象不能直接访问像以前
所以,你将能够访问仅使用受保护的数据:
-
WC_Order_Item_Product
干将方法(或与制定者的方法来改变它)... -
wc_get_order_item_meta()
专用功能。 -
WC_Data
吸气剂的方法来取消这个数据,并通过阵列访问它使用方法:-
get_data()
(此方法是最有用的) get_data_keys()
get_meta_data()
-
层的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_obj) {
echo $item_obj->get_type().'<br>'; // The order item type
echo $item_obj->get_product_id().'<br>'; // The Product ID
echo $item_obj->get_variation_id().'<br>'; // The variation ID
echo $item_obj->get_quantity().'<br>'; // Line item quantity
echo $item_obj->get_subtotal().'<br>'; // Line item subtotal
echo $item_obj->get_total().'<br>'; // Line item total
// The associated product object (which properties can't be accessed directly too)
echo '<pre>'; print_r($item_obj->get_product()); echo '</pre>';
// ... and so on ...
## Testing raw output
// echo '<pre>'; print_r($item_obj); echo '</pre>';
}
的wc_get_order_item_meta()
功能。在这里,你可以在wp_woocommerce_order_itemmeta
表和输出使用去项目ID的任何数据对应的meta_key
(对于line_item
数据类型项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_obj) {
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
// echo '<pre>'; print_r($item_obj); 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_obj) {
// Get the most useful Item product data in an accessible array
$item_data = $item_obj->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 ...
答
要获得[meta_data:protected] => Array
的数据您需要使用其他方法。
只要使用这个$item_obj->get_meta_data();
更详细的办法让他们,重复他们两次如下:
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item_obj) {
$kua = $item_obj->get_meta_data();
foreach ($kua as $key => $value) {
foreach ($value as $key2 => $value2) {
echo $key2.'->'.$value2.'<br>';
}
}
}
的方法Collection位于here
我能够与访问:$ lineItem-> get_meta_data() – bstras21
如果您可以查看我的更新答案,并附上与订购商品元数据相关的解释和多个备选方案,请在WooCommerce 3.0+ – LoicTheAztec