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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值