11

我正在尝试获取与 woocommerce 插件(wordpress)上的订单相关联的数据。目前,我已经编写了自己的包含代码的插件:

<?php 
global $woocommerce;
$order = new WC_Order($order_id);
$order_shipping_total = $order->get_shipping();
echo $order_shipping_total;
?>

这只是为了测试它,我不相信这是有效的 - 但我真正需要的是获取具有特定订单状态的订单列表,然后能够访问这些字段(如名字)对于此列表中的每个订单。我该怎么做呢?另外,我需要包含哪些文件才能完成这项工作?class-wc-order() 文件?

4

2 回答 2

22

最近我在以 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!
}
于 2013-08-14T06:29:14.557 回答
2

要过滤掉特定客户的订单,请使用附加参数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);

为特定客户加载订单的另一种方法:

$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'
) ) );

另请参见此处

于 2016-02-05T10:52:28.020 回答