Home » WooCommerce: Send Email to Admin Every 3 Hours (Cron Job)

WooCommerce: Send Email to Admin Every 3 Hours (Cron Job)

by Tutor Aspire

This snippet consists of many WooCommerce tasks: setting up a “WordPress Cron Job” (i.e. schedule a hook that runs on a specific time interval), getting the WooCommerce completed orders from the database, and finally sending a simple email to the store admin.

Complex, but as usual you can simply copy/paste and re-adapt it to your unique specifications. For example, I’m using it to send a survey email to each customer who has placed an order. There are thousands of applications, so this is just the start. Enjoy!

Setting up custom Cron Jobs in WooCommerce / WordPress

Snippet (PHP): Set up a Cron Job to Send WooCommerce Completed Orders to Admin Every 3 Hours

As I wrote in the introduction, there are 3 distinct code sections: the one where I create and schedule the cron job every 3 hours, the one where I query the database for completed orders in the last 3 hours, and the one where I generate the email.

To view and debug cron jobs in WordPress, I use “Advanced Cron Manager” from WordPress.org: https://wordpress.org/plugins/advanced-cron-manager/. Please note cron jobs won’t run unless there is frequent website traffic – otherwise it is advised to set them up via your hosting cPanel.

/**
 * @snippet       Schedule Email to WooCommerce Admin Every 3 Hours
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=106360
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://tutoraspire.com
 */ 

// ---- ---- ----
// A. Define a cron job interval if it doesn't exist

add_filter( 'cron_schedules', 'tutoraspire_check_every_3_hours' );

function tutoraspire_check_every_3_hours( $schedules ) {
    $schedules['every_three_hours'] = array(
        'interval' => 10800,
        'display'  => __( 'Every 3 hours' ),
    );
    return $schedules;
}

// ---- ---- ----
// B. Schedule an event unless already scheduled

add_action( 'wp', 'tutoraspire_custom_cron_job' );

function tutoraspire_custom_cron_job() {
if ( ! wp_next_scheduled( 'tutoraspire_woocommerce_send_email_digest' ) ) {
wp_schedule_event( time(), 'every_three_hours', 'tutoraspire_woocommerce_send_email_digest' );
}
}

// ---- ---- ----
// C. Trigger email when hook runs

add_action( 'tutoraspire_woocommerce_send_email_digest', 'tutoraspire_generate_email_digest' );

// ---- ---- ----
// D. Generate email content and send email if there are completed orders

function tutoraspire_generate_email_digest() {
$range = 180; // 3 hours in minutes
$completed_orders = tutoraspire_get_completed_orders_before_after( strtotime( '-' . absint( $end ) . ' MINUTES', current_time( 'timestamp' ) ), current_time( 'timestamp' ) );
if ( $completed_orders ) {
$email_subject = "Completed Orders Email Digest";
$email_content = "Completed Order IDs: " . implode( "|", $completed_orders );
wp_mail( '[email protected]', $email_subject, $email_content );
}
}

// ---- ---- ----
// E. Query WooCommerce database for completed orders between two timestamps

function tutoraspire_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 

You may also like