By SamXronn


2019-05-14 12:02:25 8 Comments

I'm trying to fetch all my WooCommerce orders where some additional metadata is equal to not empty.

$orders = wc_get_orders( array(
    'orderby'   => 'date',
    'order'     => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'var_rate',
            'compare' => 'NOT EXISTS',
        )
    )
));

So if the var_rate is empty or doesn't exist then do not return that record. At the moment all orders are returned using those arguments.

Example of empty record -

object(WC_Meta_Data)#2344 (2) {
  ["current_data":protected]=>
  array(3) {
    ["id"]=>
    int(6471)
    ["key"]=>
    string(15) "var_rate"
    ["value"]=>
    NULL
  }
  ["data":protected]=>
  array(3) {
    ["id"]=>
    int(6471)
    ["key"]=>
    string(15) "var_rate"
    ["value"]=>
    NULL
  }
}

2 comments

@Sally CJ 2019-05-14 15:29:34

Apparently, WooCommerce is ignoring the meta_query parameter, so that's why you're getting all orders.

You can use this code to make the meta_query parameter works with WooCommerce (orders) queries:

add_filter( 'woocommerce_get_wp_query_args', function( $wp_query_args, $query_vars ){
    if ( isset( $query_vars['meta_query'] ) ) {
        $meta_query = isset( $wp_query_args['meta_query'] ) ? $wp_query_args['meta_query'] : [];
        $wp_query_args['meta_query'] = array_merge( $meta_query, $query_vars['meta_query'] );
    }
    return $wp_query_args;
}, 10, 2 );

But as I've pointed in my comment, to query for all orders where the var_rate metadata is not empty, set the compare to !=:

'meta_query' => array(
    array(
        'key'     => 'var_rate',
        'compare' => '!=',
        'value'   => '',
    ),
),

And in your case where you are querying with a single meta query, you could simply use the meta_key parameter along with meta_compare and meta_value, without having to use the meta_query parameter:

'meta_key'     => 'var_rate',
'meta_compare' => '!=',
'meta_value'   => '',

That way, you don't have to use the code above which "enables" the meta_query parameter.

UPDATE

I think I wasn't really paying attention to the question's title.. and I thought I should add these:

In the question's title, you got "where meta data does not exist", and for that, the 'compare' => 'NOT EXISTS' or 'meta_compare' => 'NOT EXISTS' is what you would need.

But in the question's body, you said "where some additional metadata is equal to not empty" and also "if the var_rate is empty or doesn't exist then do not return that record", and for that, the 'compare' => '!=' or 'meta_compare' => '!=' is what you would need.

Because when the compare or meta_compare is set to !=, and the value or meta_value is set to '' (the default value, if not explicitly specified), then it means, "find records where the meta exists in the database and the value is not empty (not '' or not NULL)".

I hope that helps. :)

@LoicTheAztec 2019-05-14 18:10:06

The meta_query argument (that you can use in a WP_Query) is not enabled by default when using a WC_Order_Query through wc_get_orders() WooCommerce function.

But for you can use the undocumented Custom Field Parameters (just like in a WP_Query):

  • meta_key
  • meta_value
  • meta_value_num
  • meta_compare

So in your case you can use the following instead:

$orders = wc_get_orders( array(
    'limit'        => -1, // Query all orders
    'orderby'      => 'date',
    'order'        => 'DESC',
    'meta_key'     => 'var_rate', // The postmeta key field
    'meta_compare' => 'NOT EXISTS', // The comparison argument
));

This time it works nicely (Tested on WooCommerce 3.5.8 and 3.6.2).

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Filter WooCommerce Orders query with user meta data

1 Answered Questions

0 Answered Questions

Custom user query - orderby meta_key (that may not exist)

2 Answered Questions

[SOLVED] Difference between 'LIKE' and 'IN' in meta queries

2 Answered Questions

[SOLVED] Query all posts where a meta key does not exist

1 Answered Questions

Filter posts/pages by user_role array

1 Answered Questions

1 Answered Questions

2 Answered Questions

Sponsored Content