Send Woocommerce Order to email address listed on

2019-06-01 17:33发布

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条回答
放我归山
2楼-- · 2019-06-01 17:56

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 . ', me@myemail.com';
    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 <my@email.com>' . "\r\n";
    }
查看更多
Evening l夕情丶
3楼-- · 2019-06-01 18:08

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 "loic@TheAztec.com" :

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.

查看更多
登录 后发表回答