In Woocommerce I try to customize the code from this thread to add a custom email as "CC" email address in customer completed order email notification:
/**
* Function adds a BCC header to emails that match our array
*
* @param string $headers The default headers being used
* @param string $object The email type/object that is being processed
*/
function add_cc_to_certain_emails( $headers, $object ) {
// email types/objects to add cc to
$cc_email = get_user_meta( $user_id, 'order_cc_email', true ); // MY CUSTOM CODE
$add_cc_to = array(
'customer_completed_order', // Customer Processing order from WooCommerce
);
// if our email object is in our array
if ( in_array( $object, $add_cc_to ) ) {
// change our headers
$headers = array(
$headers,
// 'Cc: Me <me@example.com>' ."\r\n", // INITIAL CODE
'Cc: '.$cc_email.' <'.$cc_email.'>' ."\r\n", // MY CUSTOM CODE
}
return $headers;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_certain_emails', 10, 2 );
I can't find the way to get custom user email from user meta data, and so my code doesn't work as expected.
How to get the custom user email from user meta data?
How to add this email (with the customer full name) as "CC" in email header?
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: