简体   繁体   English

Woocommerce wp_query按ID获取订单

[英]Woocommerce wp_query get order by ID

I am trying to produce a plain output of order data. 我正在尝试生成订单数据的简单输出。 First step is a WP_QUery (perhaps) so I write this code; 第一步是WP_QUery(也许)所以我写这个代码;

$args = array (

    'post_type'  =>'shop_order',
    'posts_per_page' => -1,
    'post_status' => 'any',
    //'p' => $post_id,

);    

$order_query = new WP_Query( $args );

while ( $order_query->have_posts() ) :
  $order_query->the_post(); 

  echo the_ID();
  echo ' : '; 
  the_title();
  echo '<br/><br/>';

endwhile;

It obliging products a list of all orders, if I set the 'p' => $post_id where $post_id is a valid post ID, the query returns nothing. 它要求产品列出所有订单,如果我设置'p' => $post_id ,其中$post_id是有效的帖子ID,则查询不返回任何内容。

Any idea why, hive mind? 知道为什么,蜂巢头脑?

Alternatively is there a Woocommerce way of producing a plain page with a layout like; 或者,有一种Woocommerce方式可以生成一个布局如下的普通页面;

Order ID: 836
Order Status: ....

I assumed a WP_Query would be the obvious way but it is appearing like getting woocommerce order data is anything but straightforward. 我假设一个WP_Query将是显而易见的方式,但它看起来像获取woocommerce订单数据不是直截了当。

Update 2 更新2

To get the order data for one order, you don't need WP_query . 要获取一个订单的订单数据,您不需要WP_query You can use directly: 你可以直接使用:

$order = wc_get_order( $order_id );
$order->id; // order ID
$order->post_title; // order Title
$order->post_status; // order Status
// getting order items
foreach($order->get_items() as $item_id => $item_values){
    // Getting the product ID
    $product_id = $item_values['product_id'];
    // .../...
}

Update 1 更新1

You should try this, as with array_keys( wc_get_order_statuses() you will get all order statuses and with 'numberposts' => -1, all existing orders. 您应该尝试这一点,与array_keys( wc_get_order_statuses()您将获得所有订单状态,并且'numberposts' => -1,所有现有订单。

Here is an alternative way (without WP_query or you can use thoses args in the WP_query array): 这是另一种方法(没有WP_query或者你可以在WP_query数组中使用thgs):

$customer_orders = get_posts( array( 
    'numberposts'    => -1,
    'post_type' => 'shop_order',
    'post_status'    => array_keys( wc_get_order_statuses() ) 
) );

// Going through each current customer orders
foreach ( $customer_orders as $customer_order ) {

    // Getting Order ID, title and status
    $order_id = $customer_order->ID;
    $order_title = $customer_order->post_title;
    $order_status = $customer_order->post_status;

    // Displaying Order ID, title and status
    echo '<p>Order ID : ' . $order_id . '<br>';
    echo 'Order title: ' . $order_title . '<br>';
    echo 'Order status: ' . $order_status . '<br>';

    // Getting an instance of the order object
    $order = wc_get_order( $order_id );

    // Going through each current customer order items
    foreach($order->get_items() as $item_id => $item_values){
        // Getting the product ID
        $product_id = $item_values['product_id'];
        // displaying the product ID
        echo '<p>Product ID: '.$product_id.'</p>';
    }
}

The strict answer to this question is change 'p'=>$post_id' to 'post__in' => array($post_id) 这个问题的严格答案是将'p'=>$post_id'更改为'post__in' => array($post_id)

...but the real answer, should anyone be treading this path, is that parsing the email is so much easier, woocommerce has done most of the work for you. ...但真正的答案是,任何人都应该走这条路,就是解析电子邮件要容易得多,woocommerce已经完成了大部分工作。 There are other pratfalls along the way; 沿途还有其他一些失误; 1. The post is protected and the order notes are in the excerpt so can't be displayed ( I could move them to meta but...) 2. The order items are within comments and have to be looped then parsed to produce meaningful output, so I might as well parse the email direct. 1.帖子受到保护,订单备注在摘录中,因此无法显示(我可以将它们移动到meta但是......)2。订单项目在评论中,必须循环然后解析以产生有意义的输出,所以我不妨直接解析电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM