我正在尝试生成订单数据的简单输出.第一步是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;
如果我将’p’=>设置为,它将强制产品列出所有订单. $post_id其中$post_id是有效的帖子ID,查询不返回任何内容.
知道为什么吗?
另外,还有一种Woocommerce方式来产生具有类似布局的普通页面;
Order ID: 836
Order Status: ....
我以为WP_Query是显而易见的方法,但是它看起来像获取woocommerce订单数据一样简单.
解决方法:
更新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数组中使用那些args):
$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>';
}
}