Woocommerce - php 获取订单信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18198307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Woocommerce - php to get order information
提问by user2189190
I'm trying to get the data associated with an order on the woocommerce plugin (wordpress). Currently, I have written my own plugin that contains the code:
我正在尝试获取与 woocommerce 插件(wordpress)上的订单相关的数据。目前,我已经编写了自己的插件,其中包含以下代码:
<?php
global $woocommerce;
$order = new WC_Order($order_id);
$order_shipping_total = $order->get_shipping();
echo $order_shipping_total;
?>
This is just to test it out, I don't believe this is working- BUT what I actually need is to get a list of the orders that have a certain order status, and then be able to access the fields(like first name) for each order in this list. How do I go about doing this? Also, what files do I include to make this work? The class-wc-order() file?
这只是为了测试它,我不相信这是有效的 - 但我真正需要的是获取具有特定订单状态的订单列表,然后能够访问字段(如名字)对于此列表中的每个订单。我该怎么做?另外,我需要包含哪些文件来完成这项工作?class-wc-order() 文件?
回答by Denish
Recently i worked for Export of Orders Data in XML.
最近我为 XML 格式的订单数据导出工作。
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
// $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
回答by lubosdz
To filter out orders for a particular customer use additional argument meta_value:
要过滤特定客户的订单,请使用附加参数meta_value:
$user_id = get_current_user_id();
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'numberposts' => -1, // -1 for all orders
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
Also alternative way for loading orders for a particular customer:
为特定客户加载订单的另一种方法:
$orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => 1, // -1 for all orders
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_order',
'post_status' => 'publish'
) ) );
See also here.
另请参见此处。