具有待处理状态问题的WP_Query和WooCommerce订单

Ray*_*ger 3 php wordpress foreach orders woocommerce

我无法获得状态为wc-pending / Pending付款的订单对象。它只是返回所有订单对象:

$my_course_query = new WP_Query( array(
    'post_type'     => 'shop_order',
    'post_status'   => 'wc-pending',
    'posts_per_page'      => -1
) );
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 5

您的代码按预期工作正常,在前端,我已经对其进行了测试,并且仅输出** pending状态的订单。因此,由于您的问题没有详细说明,所以我无法确定您的问题是什么。

我在WordPress WP_Query参考上发现了此注释,该注释可能有用:
注意:票证#18408对于在管理员中查询帖子,请考虑使用get_posts(),因为wp_reset_postdata()可能无法达到预期的效果。

总的来说,我不使用WP_Query()客户订单,但是(或者也是这样):wc_get_orders() get_posts()

$customer_orders = wc_get_orders( array(
    'limit'    => -1,
    'status'   => 'pending'
) );

// Iterating through each Order with pending status
foreach ( $customer_orders as $order ) {

    // Going through each current customer order items
    foreach($order->get_items() as $item_id => $item_values){
        $product_id = $item_values['product_id']; // product ID

        // Order Item meta data
        $item_meta_data = wc_get_order_item_meta( $item_id );

        // Some output
        echo '<p>Line total for '.wc_get_order_item_meta( $item_id, '_line_total', true ).'</p><br>';
    }
}
Run Code Online (Sandbox Code Playgroud)

这也仅用于获取订单对象。

相关文档:wc_get_orders和WC_Order_Query