As I can see from WooCommerce documentation, WC_Order_Query allows only one single order status to be passed as query parameter. Has anyone find some workaround to allow querying several order statuses in the same query?

Let’s say I want to query orders with “completed” AND “refunded” status.

How can this be done via standard WC_Order_Query class?

Example from doc:

// Get orders on hold.
$args = array(
    'status' => 'on-hold',
);

$orders = wc_get_orders( $args );

Link to wc_order_query() and WC_Order_Query WooCommerce documentation

3 Answers
3

The documentation on wc_get_orders and WC_Order_Query is poor… Now regarding the order status in a WC_Order_Query, you can pass an array of order statuses:

// Display "completed" orders count
$statuses = ['completed'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

// Display "refunded" orders count
$statuses = ['refunded'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

// Display "completed" and "refunded"  orders count
$statuses = ['completed','refunded'];
$orders = wc_get_orders( ['limit' => -1, 'status' => $statuses] );
echo '<p>' . sprintf( __('Count of "%s" orders: %s'), implode('", "', $statuses), count($orders) ) . '</p>';

Tested and works.

Leave a Reply

Your email address will not be published. Required fields are marked *