Plugin Contributor
Ewout
(@pomegranate)
Hello Angel,
The problem is that at the point where the headers filter is executed, the $order is not known yet to the PDF invoice plugin because the attachment is created later.
However, the latest version of WooCommerce has a third argument to the woocommerce_email_headers filter, which holds the $order object (or $user object for user emails, etc.). Note the ‘3’ in the add_filter arguments.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $email_id, $order ) {
if ($email == 'customer_completed_order') {
$email_address_to_send = get_post_meta( $order->id, 'inscription_textbox2', true );
$headers .= 'BCC: ' . $email_address_to_send . "\r\n";
}
return $headers;
}
Important: you cannot globally apply this because for some emails (out of stock, user emails, etc.) there is not $order object available and this can throw errors.
Hope that helps!
Ewout
Hi Ewout!
Thanks a lot for your quick answer!
Actually this solution was intended only for sending the e-mail with the attached invoice to the correct e-mail address, so I guess that with the particularization you stated with $email == ‘customer_completed_order’ it should be OK to affect only this invoice attached mail, Am I right?
I will test it this evening!
Thanks again!
Plugin Contributor
Ewout
(@pomegranate)
Hi Chilidogs,
That’s correct, only I made a small error in the code, $email should be $email_id:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $email_id, $order ) {
if ($email_id == 'customer_completed_order') {
$email_address_to_send = get_post_meta( $order->id, 'inscription_textbox2', true );
$headers .= 'BCC: ' . $email_address_to_send . "\r\n";
}
return $headers;
}
Hi Ewout!
thanks a lot, the second version worked!
now What I am trying to do is to send the e-mail ONLY to the address in $email_address_to_send variable … I am trying to empty the variable $headers and afterwards to fill it with the $email_address_to_send but, I am still receiving the e-mail in the e-mail address of the user … I guess I am losing some details here … Any hint about it? Thanks a lot for your expert help!
Hi again Ewout!
For this second approach of changing the recipient of the mail it finally worked using the ‘woocommerce_email_recipient_’ filter as follows:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$email_address_to_send = get_post_meta( $object->id, 'inscription_textbox2', true );
if ( null == $email_address_to_send ){
return $recipient;
}
else{
$recipient = null;
$recipient = $email_address_to_send ;
return $recipient;
}
}
Thanks a lot for your time and effort replying so quickly!
Best regards!
Angl.