I am trying to create a function that should send a custom E-mail if a "processing order" is older than five days.
I am a bit stuck on my code. It doesn't seem to work - nothing is happening. I am also wondering how I could add order ID into the body of the custom email?
My code:
// Lookup DB for orderdate older than 5 days AND send E-mail
function expire_after_x_days(){
global $wpdb;
// Get current time
$today = date("mdy");
// set time to expire
$time_to_expire = "-5 days";
$expiration_date = date("mdy", strtotime( $today . $time_to_expire));
// Get orders with processing status
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");
if( !empty($result)) foreach ($result as $order){
// Get order's time
$order_time = get_the_time('mdy', $order->ID );
// Compare order's time with current time
if ( $order_time < $expiration_date ){
// send custom email
$to = 'test@gmail.com';
$subject = 'Test subject of my email';
$body = 'The email body content. Perhaps also write order ID';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
}
}
add_action( 'admin_footer', 'expire_after_x_days' );