Send Woocommerce Order to email address listed on product page

非 Y 不嫁゛ 提交于 2019-12-06 16:47:10

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):

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

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" :

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.

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";
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!