0

I have a custom field that contains an additional customer email account.

My idea is that when an order changes state WAIT, PENDING, PROCESSING or COMPLETED, it will reach the email of the one configured in this field.

Is this possible?

There is no problem that requires programming, but I don't know what hook to use.

Thank you.

jpussacq
  • 573
  • 9
  • 29

3 Answers3

1

This might be quite useful: https://www.skyverge.com/blog/add-woocommerce-email-recipients-conditionally/ I would take a look at the Considerations section.

yosh
  • 178
  • 15
1

You could use woocommerce_order_status_changed hook for example to notify someone each time an order change of status, like in this example:

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){          

    $to_email    = 'john.doe@gmail.com'; // <= Replace with your email custom field (the recipient)
    $shop_name   = __('Shop name'); // Set the shop name
    $admin_email = 'shop@email.com'; // Set default admin email

    $subject  = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // The subject
    $message  = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // Message content
    $headers  = sprintf( __('From: %s <%s>'), $shop_name, $admin_email ) . "\r\n"; // From admin email

    wp_mail( $to_email, $subject, $message, $headers ); // Send the email
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi LoicThe Aztec. Thanks for your answer. I think I didn't explain a part well. What I need is to send the same mail that is sent to others. It is not a notification, but send the standard Woocommerce mail. It could be CC. – jpussacq Oct 10 '19 at 18:16
  • Hi Loic, You know when you click to add product to the cart and there is some filter that do verification (like the filter `woocommerce_add_to_cart_validation`). Is there a plugin that allow this validation to be done using an ajax call in order to avoid a page refresh? I am not able to find how to search about it or probably I have to do it myself? maybe you have answered something like this already? I am pretty convinced that such thing already exist.. – Temani Afif Oct 25 '19 at 20:27
1

Add the follows code snippet in your active theme's functions.php -

function add_cc_to_wc_order_emails( $header, $mail_id, $mail_obj ) {
    $available_for = array( 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order' );
    if( in_array( $mail_id, $available_for ) ) {
        $cc_email = 'addyour@ccmail.com';
        $cc_username = "yourCCUser";
        $formatted_email = utf8_decode( $cc_username . ' <' . $cc_email . '>' );
        $header .= 'Cc: ' . $formatted_email . "\r\n";
    }
    return $header;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_wc_order_emails', 99, 3 );
itzmekhokan
  • 2,597
  • 2
  • 9
  • 9