1

I have a holiday letting wordpress site which has the advanced custom field "Supplier Email" in the add listing page. When a rental is booked the Supplier Email needs to be added to the woocommerce New Order email under BCC, i have tried numerous options but they have not worked, the Supplier Email is in the post meta so it needs to be pulled in to the new order email. This is the code i have in my functions.php file:

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' ) {
     $supplier_email = get_field( 'supplier_email_main',$post_id);

    if ( $supplier_email ) {
        $headers .= "CC: Supplier <" . $supplier_email . ">\r\n";
        $headers .= "BCC: New Order <myemail@gmail.com>" . "\r\n";
    }
}
return $headers;
}

The gmail address(this would go to my personal email, have hidden it here for obvious reasons) i added to BCC didnt even get sent. Not sure how to proceed with this, any help would be greatly appreciated, please note i am not a wordpress developer. Thanks in advance.

johnnyc0506
  • 133
  • 1
  • 3
  • 12
  • _"the Supplier Email is in the post meta"_ - is this stored via a product ID or the order ID? did you try replacing `get_field( 'supplier_email_main',$post_id);` with `get_field( 'supplier_email_main', $order->get_id() );` – 7uc1f3r Jul 28 '22 at 11:17
  • I looked through the database and the supplier_email_main is in post_meta, the listings are posts, would $order->get_id() find the supplier_email_main acf field? – johnnyc0506 Jul 28 '22 at 13:08
  • Well, since you're using plugins (which I don't use) it's hard to determine what you're specifically looking for. Can you confirm that the supplier_email_main post ID matches the product ID? you will have to obtain the desired data via the `$order` object, only the question is how you can make the connection between the post ID and the order ID, hence my question if you can determine the ID – 7uc1f3r Jul 29 '22 at 07:57

1 Answers1

1

try this

add_filter( 'woocommerce_email_headers', 'add_bcc_header', 10, 3 );
function add_bcc_header( $headers, $id, $order ) {
    if ( $id == 'new_order' ) {
        $headers .= 'Bcc: gmailaddress@gmail.com' . "\r\n";
    }
    return $headers;
}
c0d3x27
  • 193
  • 2
  • 15