Query WooCommerce orders grouped by several order statuses

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 Comment