Add custom user email to CC for specific Woocommerce email notification

眉间皱痕 提交于 2019-12-01 14:42:18

There is some missing arguments in your hooked function, as woocommerce_email_headers filter hook allow 3 arguments:

  • $header ===> the header data to be return in this filter
  • $email_id==> the current WC_Email ID (but not the $object…)
  • $order ====> the instance of the WC_Order object (the missing useful one)

Try this revisited code instead:

add_filter( 'woocommerce_email_headers', 'custom_cc_email_headers', 10, 3 );
function custom_cc_email_headers( $header, $email_id, $order ) {

    // Only for "Customer Completed Order" email notification
    if( 'customer_completed_order' !== $email_id )
        return $header;

    // Get the custom email from user meta data  (with the correct User ID)
    $custom_user_email = get_user_meta( $order->get_user_id(), 'order_cc_email', true );

    if( ! empty($custom_email) ) 
        return $header; // Exit (if empty value)

    // Get customer billing full name
    $user_name  = $order->get_billing_first_name().' ';
    $user_name .= $order->get_billing_last_name();

    // Merge and prepare the data
    $formatted_email = utf8_decode($user_name . ' <' . $custom_user_email . '>');

    // Add Cc to headers
    $header .= 'Cc: '.$formatted_email .'\r\n';

    return $header;
}

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

Related threads:

I have modified the above code to include a second CC email address which is working for all notifications including a couple of custom emails I have set up, so thanks for the original code!

add_filter( 'woocommerce_email_headers', 'custom_cc_email_headers', 10, 3 );
function custom_cc_email_headers( $headers, $email_id, $order ) {

// Only for "Customer Completed Order" email notification
// if( 'wcj_custom_1' !== $email_id )
//    return $headers;

// Get the custom email from user meta data  (with the correct User ID)
$custom_user_email = get_user_meta( $order->get_user_id(), 'doc_email', true );
$bcc_email = get_user_meta( $order->get_user_id(), 'admin_email', true );

if( ! empty($custom_email) ) 
    return $headers; // Exit (if empty value)

// Get customer billing full name
$user_name  = $order->get_billing_first_name().' ';
$user_name .= $order->get_billing_last_name();

// Merge and prepare the data
$formatted_email = utf8_decode($user_name . ' <' . $custom_user_email . '>');
$formatted_email2 = utf8_decode($user_name . ' <' . $bcc_email . '>');

// Add Cc to headers
$headers .= 'Cc: '.$formatted_email ."\r\n";
$headers .= 'Cc: '.$formatted_email2."\r\n";

return $headers;
}

I do have a couple of questions, why is the below var declared as

$custom_email

and not, in the if statement

$custom_user_email

Also, would it also be possible to make this into an array of email IDs to apply the cc addresses to?

// Only for "Customer Completed Order" email notification
// if( 'wcj_custom_1' !== $email_id )
//    return $headers;

As in as array like this (found here)

$email_ids = array(
   'new_order',
   'cancelled_order',
   'failed_order',
   'customer_on_hold_order',
   'customer_processing_order',
   'customer_completed_order',
   'customer_refunded_order',
   'customer_invoice',
   'customer_note',
   'customer_reset_password',
   'customer_new_account',
  );

I have tried to incorporate these ideas into the existing code, but failing to get it to work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!