2017-02-18 163 views
2

我正在嘗試生成訂單數據的純輸出。第一步是WP_QUERY(也許),所以我寫這個代碼;Woocommerce wp_query通過ID獲取訂單

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

它樂於助人的產品全部訂單的名單,如果我設置'p' => $post_id其中$post_id是一個有效的郵件ID,無返回值。

任何想法爲什麼,蜂巢介意?

可選地是有製造具有佈局像一個普通的頁面的Woocommerce方式;

Order ID: 836 
Order Status: .... 

我假設一個WP_Query將是顯而易見的方式,但它看起來像獲取woocommerce的訂單數據是非常簡單的。

+0

作爲背景,我有一個從Romancart購物解決方案,我可以解析封電子郵件的Filemaker數據庫。當然,解析來自Woocommerce的訂單電子郵件,但我想我會有點聰明,並通過網頁傳遞數據。 ESS當然是最好的解決方案,但它也是編程密集型的。 –

+0

這可能有助於http://stackoverflow.com/a/28847572/1182891 –

回答

4

更新2

爲了拿到訂單數據一階,你不需要WP_query。您可以直接使用:

$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']; 
    // .../... 
} 

更新1

你應該試試這個,與array_keys(wc_get_order_statuses()你會得到所有的訂單狀態,並與'numberposts' => -1,所有現有訂單。

這是一種替代方法(不WP_query也可以將WP_query陣列中使用放入系統參數):

$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>'; 
    } 
} 
+0

'「post_status」 =>'any''的伎倆,返回所有的帖子不是一個問題。返回一篇文章是我的目標,請參閱上面的答案。不過謝謝你花時間回答。 –

0

嚴格的這個問題的答案是改變'p'=>$post_id''post__in' => array($post_id)

...但真正的答案是,任何人都應該走這條道路,解析電子郵件是非常容易的,woocommerce已經爲你完成了大部分工作。沿途還有其他一些事物; 1.帖子受到保護,訂單註釋在摘錄中,因此無法顯示(我可以將它們移動到meta,但是...)2.訂單項在註釋中,必須循環,然後解析以生成有意義的輸出,所以我不妨直接解析電子郵件。

+0

值得一提的,因爲移動Woocommerce數據的Filemaker是一個東西的人可能想要做的,是一個事實,即一個Woocommerce順序是相互關聯的表項的糾結是指任何解決方案將是複雜的,而且訂單電子郵件可能來自訂單的完整數據集的最佳訂購彙總。 –

+0

你的問題很不清楚......我不明白你試圖打一個訂單。你應該直接使用這個'$ order = wc_get_order($ order_id);'...不需要查詢...然後如果你使用'print_r($ order)',你會看到你可以得到永恆...我已經更新了我的答案2次。 – LoicTheAztec