Site icon Anthony Carbon

Add BCC email in Woocommerce new order via product post meta

Add BCC email in Woocommerce new order via product post meta? How to do that? Create a custom field with a meta_key of ’email_address’ and put the codes to your functions.php

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2 );
function mycustom_headers_filter_function( $headers, $object ) {
 	if( $object == 'new_order' ){
 		global $woocommerce;
		$cart = $woocommerce->cart;
        if(!empty($cart)){
            $group = array();
            foreach($cart->cart_contents as $val){
                $email = '<'.get_post_meta( $val['product_id'],'email_address', true ).'>';
                $group[] = $email;
            }
            $email_add = join(', ',array_unique ($group));
            $headers .= 'BCC: My name '. $email_add .'' . "\r\n";
        }
    }
    return $headers;
}
Exit mobile version