Add Custom User Email to Cc for Specific Woocommerce Email Notification

Add custom user email to CC for specific Woocommerce email notification

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:

  • Get the customer ID from an order ID in WooCommerce
  • Adding custom emails to BCC for specific Woocommerce email notifications

Adding BCC recipient to WooCommerce email notification from ACF option

There are some mistakes in your code… You need to check that you get a value when using your ACF custom field: get_field( 'manufacturer_email_recipient', 'options' ).

If its is the case, try the following:

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

if ( $email_id === 'new_order' ) {
$manufacturer_email = get_field( 'manufacturer_email_recipient', 'options' );

if ( $manufacturer_email ) {
$headers .= "BCC: Manufacture <" . $manufacturer_email . ">\r\n";
}
}
return $headers;
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

Related: New Account Email Notification sent to Admin as BCC in Woocommerce

Woocommerce email notifications additional recipients based on user role

For user roles based, try the following:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) )
return $recipient;

// Get an instance of the WP_User Object related to the order
$user = $order->get_user();

// Add additional recipient based on custom user roles
if ( in_array( 'user_role1', $user->roles ) )
$recipient .= ', email1@gmail.com';
elseif ( in_array( 'user_role2', $user->roles ) )
$recipient .= ', email2@gmail.com';

return $recipient;
}

Code goes in function.php file of your active child theme (or active theme). It should works.

Adding custom emails to BCC for specific Woocommerce email notifications

As 'wc_course_order' is a custom email notification ID that I can't really test it with it (so for testing purpose I have commented it, when testing the function myself)…

Using the same way than you to get the email, I suppose that I am getting the first and last name in the code below (but I am not absolutely sure)

Now to add this emails as BCC, you will have to change of hook:

add_filter( 'woocommerce_email_headers', 'student_email_notification', 20, 3 );
function student_email_notification( $header, $email_id, $order ) {
// Only for 'wc_course_order' notification
if( 'wc_course_order' != $email_id ) return $header;

$student_emails = array();
$enroll_num = 0;

// Loop though Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
$course_qty = $item_data->get_quantity();
$q = 1;
while ( $q <= $course_qty){
$enroll_num++;
// Get the student full Name
$full_name = wc_get_order_item_meta( $item_id, 'First Name - '.$enroll_num, true );
$full_name .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - '.$enroll_num, true );
// Get the student email
$student_email = wc_get_order_item_meta( $item_id, 'Student Email - '.$enroll_num, true );
if( ! empty($student_email) && $full_name != ' ' )
// Format the name and the email and set it in an array
$student_emails[] = utf8_decode($full_name . ' <' . $student_email . '>'); // Add name + email to the array
$q++;
}
}

// If any student email exist we add it
if( count($student_emails) > 0 ){
// Remove duplicates (if there is any)
$student_emails = array_unique($student_emails);
// Add the emails to existing recipients
$header .= 'Bcc: ' . implode(',', $student_emails) . "\r\n";
}
return $header;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works
(I can't really test your code at 100%, but it should work, I hope).


Related: How to get order ID in woocommerce_email_headers hook

Send new order email to CC if order has items from a certain category in WooCommerce

  • The use of wc_get_order() is not necessary, you can already access the $order object via the parameters
  • $orderid['product_cat'] does not exist
  • Explanation via comments added in the code

So you could use

function filter_woocommerce_email_headers( $header, $email_id, $order ) {
// New order
if ( $email_id == 'new_order' ) {
// Set categories
$categories = array( 'categorie-1', 'categorie-2' );

// Flag = false by default
$flag = false;

// Loop trough items
foreach ($order->get_items() as $item ) {
// Product id
$product_id = $item['product_id'];

// Has term (a certain category in this case)
if ( has_term( $categories, 'product_cat', $product_id ) ) {
// Found, flag = true, break loop
$flag = true;
break;
}
}

// True
if ( $flag ) {
// Prepare the the data
$formatted_email = utf8_decode('My test <mytestemail@test.com>');

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

return $header;
}
add_filter( 'woocommerce_email_headers', 'filter_woocommerce_email_headers', 10, 3 );


Related Topics



Leave a reply



Submit