Skip to content
Advertisement

Adding BCC recipient to WooCommerce email notification from ACF option

Using Advanced Custom Fields plugin, I have created a custom field in the WordPress options tab that allows my client to add an additional email which will receive Woocommerce new order email confirmation, as well as themselves.

The function below is working if i hardcode the email address. Although i want the additional email recipient to pull from the ACF options which the client can add/change etc. The email is pulling through correctly from options ACF. Although when i try to concatenate the email to the BCC the new email doesn’t receive the new order email.

Can anyone point out where im going wrong with this please? Thanks

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object) {

    $additional_order_email_recipient = get_field('manufacturer_email_recipient', 'options');
    $manu_email = "&lt".$additional_order_email_recipient."&gt";
    $email_header = "BCC: Manufacture " . $manu_email . "rn";

    if ($object == 'new_order') {

        $headers .= "BCC: New Order <email@domain.co.uk>" . "rn"; // This Works
        $headers .= "BCC: New Order " . $manu_email . "rn"; // This doesnt work
        $headers .= $email_header; // This doesnt work

    }
    return $headers;
}

Advertisement

Answer

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 . ">rn";
        }
    }
    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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement