Woocommerce - php获取订单信息

时间:2013-08-12 23:29:31

标签: php wordpress woocommerce

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

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

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

2 个答案:

答案 0 :(得分: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!
}

答案 1 :(得分:2)

要过滤掉特定客户的订单,请使用其他参数 meta_value

 if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
            if (isInEditMode()) {
                // Don't crash the layout editor. Consume all of the space if specified
                // or pick a magic number from thin air otherwise.
                // TODO Better communication with tools of this bogus state.
                // It will crash on a real device.
                if (widthMode == MeasureSpec.AT_MOST) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                    widthMode = MeasureSpec.EXACTLY;
                    widthSize = 300;
                }
                if (heightMode == MeasureSpec.AT_MOST) {
                    heightMode = MeasureSpec.EXACTLY;
                }
                else if (heightMode == MeasureSpec.UNSPECIFIED) {
                    heightMode = MeasureSpec.EXACTLY;
                    heightSize = 300;
                }
            } else {
                throw new IllegalArgumentException(
                        "DrawerLayout must be measured with MeasureSpec.EXACTLY.");
            }
        }

另一种为特定客户加载订单的替代方式:

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

另见here