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

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 CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=106360
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @community     https://businessbloomer.com/club/

// ---- ---- ----
// 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,
        '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' ) );
	if ( $completed_orders ) {
		$email_subject = "Completed Orders Email Digest";
		$email_content = "Completed Order IDs: " . implode( "|", $completed_orders );
		wp_mail( 'your@email.com', $email_subject, $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(
			"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;

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Content to a Specific Order Email
    Customizing WooCommerce emails via the WordPress dashboard is not easy and – sometimes – not possible. For example, you can’t edit or add content to them unless you’re familiar with code. Well, here’s a quick example to learn how to add content to any WooCommerce default order email. In this case study, our goal is […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Get Order Data (total, items, etc) From $order Object
    As a WooCommerce development freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?“. For example, “How can I get the order total“? Or “How can I get the order items“? Or maybe the order dates, customer ID, […]
  • WooCommerce Visual Hook Guide: Emails
    WooCommerce customizers: the Visual Hook Guide is back! Here’s a visual HTML hook guide for the WooCommerce Emails. This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations. Let me know in the comments if this […]
  • WooCommerce: Allow Users to Edit Processing Orders
    How can WooCommerce customers edit an order they just placed and paid for? I swear I looked on search engine results and other places before coming to the conclusion I needed to code this myself. For example, a user might want to change the delivery date (if you provide this on the checkout page). Or […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

11 thoughts on “WooCommerce: Send Email to Admin Every 3 Hours (Cron Job)

  1. How would I change this to send a digest of new orders instead of completed orders? Would I change $completed_orders to $new_orders and wc-completed to wc-new?

    1. Not really. Which is the order status for new orders (e.g. on-hold)?

  2. How can we send this email using the WooCommerce email template?

    1. Hi Ravi, try with a variation of this: https://businessbloomer.com/woocommerce-send-custom-email/

  3. This can be done without an extra Cron plugin, using the built-in Scheduled Actions function in WooCommerce(since 3.5):

    add_action( 'init', 'wc_setup_admin_email_cron' );
    function wc_setup_admin_email_cron() {
        $queue = WC()->queue();
        $next  = $queue->get_next( 'wc_admin_email_send' );
        if ( ! $next ) {
            $queue->schedule_recurring( time(), HOUR_IN_SECONDS*3, 'wc_admin_email_send' );
        add_action( 'wc_admin_email_send', 'wc_send_admin_email_with_cron' );
    function wc_send_admin_email_with_cron() {
    //function that sends the emails
    1. Fantastic!

      1. For example I can add


        filter hook too? Or it’s only working with action hooks?

        1. Surely there is a way

    2. thank you so much to both, you guys saved my life.

  4. thanks,
    this is very helpful post

    1. Great to hear that!

Questions? Feedback? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *