Filter WooCommerce Orders query with user meta data

Question

I want to list orders from my Woocommerce shop. I have user meta for my users. (unsubscribed) I only want to list the order if that order’s user is unsubscribed === 0.

I have working code to the customers:

$args = array(
    'role'       => 'customer',
    'number'     => - 1,
    'meta_query' => [
        'relation' => 'OR',
        [
            'key'     => 'unsubscribed',
            'compare' => '!=',
            'value'   => 1
        ],
        [
            'key'     => 'unsubscribed',
            'value'   => 0,
            'compare' => 'NOT EXISTS'
        ]
    ],
);

$query  = new WP_User_Query( $args );

I would like to do similar query with wc_get_orders but it doesn’t work:

$args   = [
    'limit' => - 1,
    'meta_query' => [
        'relation' => 'OR',
        [
            'key'     => 'unsubscribed',
            'compare' => '!=',
            'value'   => 1
        ],
        [
            'key'     => 'unsubscribed',
            'value'   => 0,
            'compare' => 'NOT EXISTS'
        ]
    ]
];

$orders = wc_get_orders( $args );
0
, , landorid 1 year 2020-07-12T19:10:21-05:00 0 Answers 68 views 0

Leave an answer

Browse
Browse