I am using woocommerce to build a website where people can post gigs (products) and people interested in those gigs can pay to contact who posted the gig. I have a custom field called customer_email in the product edit page where I save the email of the person that posted the gig and I am able to pass the value of that field to the order meta each time the gig (product) is bought. Now what I want to do is to be able to send an automatic review email to the customer_email field in order meta two weeks after a gig (product) is bought, and I want the email content to include a link to the profile of the person that bought the gig (product).

Example email:

To: [email protected] (customer_email field in the product and order meta)

Hello Michael (customer_name field in the product and order meta)

Socratis Engineering (name of the person that bought the product) contacted you about the job you posted on our platform. Did you hire them? Please take a moment to review their work if you did.

https://example.com/socratis-engineering/review (link to the profile review page of the person that bought the product)

Kind regards, The 123 Team

I have been exploring the use of wordpress cron job to achieve this but the first problem I face is that I cannot get the relevant order meta value from woocommerce. I want the email to be scheduled and sent for every order but I just can't figure it out. I am very new to wordpress and php.

I am also open to a plugin solution if anyone knows a plugin that can do what I am asking. I will highly appreciate your help.

1 Answer 1


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?

 * @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(
            "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.

  • Thank you Antti. Your code helped me a lot
    – Sadiq11111
    Mar 18, 2019 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.