4

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

0

3 Answers 3

9

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.

1

Instead of passing 'status' parameter, you can use 'post_status' parameter as an array like this:

// Get orders on hold and process
$args = array(
    'post_status' => array('wc-processing','on-hold')
);

$orders = wc_get_orders( $args );
0

It took me a while to understand for wordpress + woocommerce but this works perfect :)

if (!function_exists('custom_filter_for_shop_order')) {

    function custom_filter_for_shop_order($query)
    {
        global $typenow;

        if (
            is_admin() && $query->is_main_query() && in_array($typenow, wc_get_order_types('order-meta-boxes'), true)
            && isset($_GET['filter_status']) && is_array($_GET['filter_status'])
        ) {

            $query->set('post_status', $_GET['filter_status']);

            remove_filter('pre_get_posts', 'custom_filter_for_shop_order');
        }
    }
    add_filter('pre_get_posts', 'custom_filter_for_shop_order');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.