Raymond Seger
Raymond Seger

Reputation: 1130

WP_Query and WooCommerce orders with pending status issue

I can't get order objects with status of wc-pending / Pending Payment. It simply returns ALL the order objects:

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

Upvotes: 3

Views: 13747

Answers (3)

Alchem
Alchem

Reputation: 151

I do have the same issue (returning ALL Orders) while debugging.

Wrapping the debug-code into an action helped outputting the expected data:

add_action( 'init', 'debug_init' );

function debug_init() {

    $custom_query_args = array(
        "fields" => "ids",
        "post_type" => "shop_order",
        "post_status" => array('wc-processing'),
        "posts_per_page" => "-1",
        "offset" => "0",
        "date_query" => [
                "before" => "2020-09-10 23:59",
                "after" => "1970-01-01 00:00",
                "inclusive" => "1"
        ],
        "order" => "DESC"
    );


    $debugQuery = new WP_Query( $custom_query_args );
    $order_ids   = $debugQuery->posts;

    print_r($order_ids);

    die();
}

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253939

Your code Is just working perfectly as expected, in frontend, I have test it and it output only orders with **pending status. So I can't tell what is your issue as your question is not detailed.

I have found this note on WordPress WP_Query reference that could be useful:
Note: Ticket #18408 For querying posts in the admin, consider using get_posts() as wp_reset_postdata() might not behave as expected.

In general, I don't use WP_Query() for customer orders but wc_get_orders() (or get_posts() too) this way:

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

This works also just to get the orders objects.

Related documentation: wc_get_orders and WC_Order_Query

Upvotes: 9

Raymond Seger
Raymond Seger

Reputation: 1130

I fixed this weird issue by simply using custom query.

Somehow adding 'post_status' => 'wc-pending' doesn't actually change the query, but if I use 'post_status' => 'pending', the query changes.

So what I did was using that custom query and modify pending to wc-pending.

Upvotes: 1

Related Questions