Send automatic Email to the Customer by Order depending on the week days in WooCommerce

Is it possible to send an email to customers by order depending on the week days in WooCommerce?

For example: If some make order on Monday, Tuesday and Wednesday then the first email is send.

Read More

else if some make order Thursday, Friday and Saturday then the second email will be send.

Related posts

Leave a Reply

1 comment

  1. (updated) – First you need to find the current day of the week using php function date() this way:

    $today= date('L');
    

    Then we need to define the days for first email action and for 2nd email action storing this days in an array:

    $days1 = array( 'monday', 'tuesday', 'Wednesday' );
    $days2 = array( 'thursday', 'friday', 'saturday' ); 
    

    Now we need to compare the current day $today with $days1 and **$days**2 to make an action:

    if ( in_array( $today, $days1 ) ) {
        // do something
    } else if {
        // do something else
    } else {
        exit; // do nothing
    }
    

    Now, for example, we can use the hook of this answer to your question combining the previous with it, this way:

    add_action( 'woocommerce_payment_complete', 'order_completed' )
    function order_completed( $order_id ) {
        $today= date('L');
        $days1 = array( 'monday', 'tuesday', 'Wednesday' );
        $days2 = array( 'thursday', 'friday', 'saturday' ); 
        $user_email = $current_user->user_email;
        $to = sanitize_email( $user_email );
        $headers = 'From: Your Name <your@email.com>' . "rn";
        if ( in_array( $today, $days1 ) ) {
            wp_mail($to, 'subject', 'This is custom email 1', $headers );
        } elseif ( in_array( $today, $days2 ) ) {
            wp_mail($to, 'subject', 'This is custom email 2', $headers );
        } else {
            exit; // do nothing
        }
    }
    

    You can use on of this hooks too depending on your needs, and you can even combine them together:

    add_action( 'woocommerce_order_status_pending', 'my_custom_action');
    add_action( 'woocommerce_order_status_failed',  'my_custom_action');
    add_action( 'woocommerce_order_status_on-hold', 'my_custom_action');
    add_action( 'woocommerce_order_status_processing', 'my_custom_action');
    add_action( 'woocommerce_order_status_completed', 'my_custom_action');
    add_action( 'woocommerce_order_status_refunded', 'my_custom_action');
    add_action( 'woocommerce_order_status_cancelled', 'my_custom_action');
    add_action( 'woocommerce_payment_complete', 'my_custom_action' ); // Using this one
    add_action( 'woocommerce_thankyou', 'my_custom_action' ); // this could be convenient too
    function my_custom_function($order_id) {
        // your code goes here
    }
    

    NOTE: All this code goes on function.php file of your active child theme or theme