It's getting quite late here as I'm typing this so take the code below with a grain of salt.
I think if you modify the code provided here (modified version below for future reference), https://businessbloomer.com/woocommerce-custom-cron-job/ (I'm not affiliated to the site), you should get the thing done.
The thing you need to change is the part D. Add a foreach loop to go through all the matching orders. Get the required order and custom meta data with the order id, create the email and send it. You could use a helper function for the email body creation or you can do it inside the loop - a matter of taste, I think.
You might also want to change the scheduling. It's set to three hours on the original code example. In your case maybe once daily would be more appropriate, yes?
<?php
/**
* @snippet Schedule Email to WooCommerce Admin Every 3 Hours
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=106360
* @author Rodolfo Melogli
* @compatible WooCommerce 3.5.4
*/
// ---- ---- ----
// A. Define a cron job interval if it doesn't exist
add_filter( 'cron_schedules', 'bbloomer_check_every_3_hours' );
function bbloomer_check_every_3_hours( $schedules ) {
$schedules['every_three_hours'] = array(
'interval' => 10800, // <= you might want to change this to something less frequent, once daily?
'display' => __( 'Every 3 hours' ),
);
return $schedules;
}
// ---- ---- ----
// B. Schedule an event unless already scheduled
add_action( 'wp', 'bbloomer_custom_cron_job' );
function bbloomer_custom_cron_job() {
if ( ! wp_next_scheduled( 'bbloomer_woocommerce_send_email_digest' ) ) {
wp_schedule_event( time(), 'every_three_hours', 'bbloomer_woocommerce_send_email_digest' );
}
}
// ---- ---- ----
// C. Trigger email when hook runs
add_action( 'bbloomer_woocommerce_send_email_digest', 'bbloomer_generate_email_digest' );
// ---- ---- ----
// D. Generate email content and send email if there are completed orders
function bbloomer_generate_email_digest() {
$range = 180; // 3 hours in minutes
$completed_orders = bbloomer_get_completed_orders_before_after( strtotime( '-' . absint( $end ) . ' MINUTES', current_time( 'timestamp' ) ), current_time( 'timestamp' ) ); // Change parameters to compare desired time period
if ( $completed_orders ) {
// Untested, but something like this should work
foreach ( $completed_orders as $completed_order_id ) {
$customer_email = get_post_meta( $completed_order_id, 'customer_email', true );
$order = wc_get_order( $completed_order_id );
if ( $customer_email ) {
$email_subject = "Gig feedback";
$email_content = get_email_content( $order );
wp_mail( $customer_email, $email_subject, $email_content );
}
}
}
}
// Helper function to make the email loop a little cleaner
function get_email_content( $order ) {
// Return email content
}
// ---- ---- ----
// E. Query WooCommerce database for completed orders between two timestamps
function bbloomer_get_completed_orders_before_after( $date_one, $date_two ) {
global $wpdb;
$completed_orders = $wpdb->get_col(
$wpdb->prepare(
"SELECT posts.ID
FROM {$wpdb->prefix}posts AS posts
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'wc-completed'
AND posts.post_modified >= '%s'
AND posts.post_modified <= '%s'",
date( 'Y/m/d H:i:s', absint( $date_one ) ),
date( 'Y/m/d H:i:s', absint( $date_two ) )
)
);
return $completed_orders;
}
P.s. I'll try to remember to have a second look at this tomorrow.