1
votes

I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.

Any thoughts on maybe how to modify the functions.php file to do this?

2
As you can have many items for an order, this additional email field should not be a product custom field, but an additional checkout field. Think it…LoicTheAztec
There may be more than one product in the cart but thr email is being requested for on the product page itself as a "first step". It is assumed that the confirmation of a purchase will always go to the same address, but it could differ from the billing address, which is why it needs to appear on the product page.MacGyver_97

2 Answers

1
votes

In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.

But first Lets add globally an email field with the Product Add-ons plugin…

  1. The add on field on the product (fill the field and add to cart):

enter image description here

  1. This "Email" field in order-received (Thank you) page, after checkout:

enter image description here

As you can notice the label of this field is "Email"…

Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "[email protected]" :

enter image description here

Now I can set the correct meta_key in the code below to get my email.

Here is the code that will add this additional email recipient for processing and completed customer order email notifications:

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    $additional_recipients = array(); // Initializing…

    // Iterating though each order item
    foreach( $order->get_items() as $item_id => $item_data ){
        // HERE set the the correct meta_key (like 'Email') to get the correct value
        $email = wc_get_order_item_meta( $item_id, 'Email', true );

        // Avoiding duplicates (if many items with many emails)
        // or an existing email in the recipient
        if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
            $additional_recipients[] = $email;
    }

    // Convert the array in a coma separated string
    $additional_recipients = implode( ',', $additional_recipients);

    // If an additional recipient exist, we add it
    if( count($additional_recipients) > 0)
        $recipient .= ','.$additional_recipients;

    return $recipient;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

0
votes

You can add below code in your function.php

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);

function your_email_recipient_filter_function($recipient, $object) {
    $recipient = $recipient . ', [email protected]';
    return $recipient;
}

and if you want to send email in BCC then please try below code:

add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);

function mycustom_headers_filter_function( $headers, $object ) {
    if ($object == 'customer_completed_order') {
        $headers .= 'BCC: My name <[email protected]>' . "\r\n";
    }