douzui6173 2017-02-18 16:30
浏览 152
已采纳

Woocommerce wp_query按ID获取订单

I am trying to produce a plain output of order data. First step is a WP_QUery (perhaps) so I write this code;

$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.

Any idea why, hive mind?

Alternatively is there a Woocommerce way of producing a plain page with a layout like;

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.

  • 写回答

2条回答 默认 最新

  • duanjiao6731 2017-02-18 17:15
    关注

    Update 2

    To get the order data for one order, you don't need 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

    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.

    Here is an alternative way (without WP_query or you can use thoses args in the WP_query array):

    $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>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛