Extend wc_get_orders() with a custom meta key and meta value
Asked Answered
P

2

7

I'm trying to get all the shop orders with a meta key "order_referrer_id" and the value will be a user id for example "1060".

This is what i have tried so far:

$args = array(
        'limit' => -1,
        'status' => 'completed',
        'meta_query' => array(
            array(
                'key' => 'order_referrer_id',
                'value' => 1060,
                'compare' => '='
            ),
        ),
        'return' => 'ids',
    ); 

$orders = wc_get_orders( $args );

For some reason the query ignore the meta query and returns all the orders.

What is the correct way to filter shop orders?

Preset answered 11/3, 2022 at 12:43 Comment(1)
wc_get_orders does not take all the same arguments, as f.e. get_posts would. You will have to do it this way, github.com/woocommerce/woocommerce/wiki/…Recalcitrant
T
15

wc_get_orders and WC_Order_Query provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.

Source: wc_get_orders and WC_Order_Query


You can use meta_key, meta_value and meta_compare, so you get:

$args = array(
    'status'        => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
    'meta_key'      => 'order_referrer_id', // Postmeta key field
    'meta_value'    => 1060, // Postmeta value field
    'meta_compare'  => '=', // Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’.
    'return'        => 'ids' // Accepts a string: 'ids' or 'objects'. Default: 'objects'.
);

$orders = wc_get_orders( $args );

// NOT empty
if ( ! empty ( $orders ) ) {  
    foreach ( $orders as $order ) {
        echo '<p>ID = ' . $order . '</p>';
    }
}
Twospot answered 11/3, 2022 at 13:6 Comment(0)
A
1

Cleanest way to do this, is to create a custom parameter which you can use to create your WC_Order_Query. As mentioned in the docs here: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#adding-custom-parameter-support

add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'custom_order_query', 10, 2 );

function custom_order_query($query, $query_vars){
    
    if ( ! empty( $query_vars['order_referrer_id'] ) ) {
    
        $query['meta_query'][] = array(
            'key' => 'order_referrer_id',
            'value' => esc_attr( $query_vars['order_referrer_id'] ),
            'compare' => '='
        );
    
    }
    
    return $query;

}

Then you can use it like this:

$orders = wc_get_orders(array(
    
    'limit' => -1,
    'status' => 'completed',
    'order_referrer_id' => 1060,
    'return' => 'ids',
    
));
Accrual answered 24/10, 2022 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.