You can do it by using your WP_User_Query in a WC_Order_Query with the customer_id argument this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // Users query $user_ids = (array) get_users([ 'role' => 'customer', 'number' => - 1, 'fields' => 'ID', 'meta_query' => [ 'relation' => 'OR', [ 'key' => 'unsubscribed', 'compare' => '!=', 'value' => 1 ], [ 'key' => 'unsubscribed', 'compare' => 'NOT EXISTS' ] ], ]); // Orders query (using the users IDs from the user query) $orders = wc_get_orders([ 'limit' => - 1, 'status' => ['on-hold','processing','completed'], 'customer_id' => $user_ids, ]); // Loop through Order IDs foreach( $orders as $order ) { // Get the Order ID $order_id = $order->get_id(); // And so on … } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.