2

I am building a site with Woocommerce and YITH Woocommerce Booking. I have added an ACF field to include a different email in each Booking and send mail from the new Booking to the email included in the ACF field and the administrator email.

When I directly add an email in my code instead of the variable it works perfectly, but when I change it back to take the email from the ACF field it doesn't work for me.

I have the following code in my functions.php file but it doesn't work for me. What can I have wrong?


add_filter( 'woocommerce_email_headers', 'new_booking_client', 9999, 3 );
 
function new_booking_client( $headers, $email_id, $order ) {
    if ( 'yith_wcbk_admin_new_booking' == $email_id ) {
        $client_email = get_field( 'e_mail_prestador');
        $headers = "BCC: Name <" . $client_email . ">\r\n";            
    };
    return $headers;
}

1 Answers1

0

Looking at the code which you have shared with your question, I can assure you that you are headed in the right direction. However, you are missing a couple of things. The hook woocommerce_email_headers is providing you with three parameters:

  1. $headers is the variable which contains the email headers and you have rightly tried to edit it to add BCC parameter to it.
  2. $email_id contains a unique identifier to check which email event has been triggered. yith_wcbk_admin_new_booking is the correct value.
  3. $order contains the object which was created before triggering this email event. In this case it should be a yith_booking object.

You haven't used the yith_booking object to get the product from it along with the ACF field connected to that product which is why it isn't working. The function get_field( 'e_mail_prestador') doesn't know which product's ACF field you are trying to access hence it doesn't provide the correct email address to be added in the headers.

According to all the explanation above, the code which should work correctly is as follows:

add_filter( 'woocommerce_email_headers', 'add_bcc_to_headers_from_prod_acf', 20, 3 );
function add_bcc_to_headers_from_prod_acf( $headers, $email_id, $yith_booking) {
    if ( $email_id === 'yith_wcbk_admin_new_booking' ){
        if($yith_booking){
            $product_id = $yith_booking->get_product_id();
            $field_val = get_field('e_mail_prestador', $product_id, false);
            $headers .= 'Bcc: ' . $field_val . "\r\n";
        }
    }
    return $headers;
}
Jordan
  • 713
  • 15
  • 30