Add account email conditionally on Customer processing Order email notification in Woocommerce

此生再无相见时 提交于 2019-12-06 14:55:46

You can't get the current user or the current user ID on email notification hooks.
You need first to get the customer ID from the order, then you can get the WP_User object to get the account email.

There is 2 different ways to add the customer account email when it's different from the order billing email in Customer processing order email notification:

1) Add the customer account email as an additional recipient:

add_filter( 'woocommerce_email_recipient_customer_processing_order', 'add_customer_processing_order_email_recipient', 10, 2 );
function add_customer_processing_order_email_recipient( $recipient, $order ) {
    // Not in backend (avoiding errors)
    if( is_admin() ) return $recipient;

    if( $order->get_customer_id() > 0 ){

        // Get the customer WP_User object
        $wp_user = new WP_User($order->get_customer_id());

        if ( $wp_user->user_email != $order->get_billing_email() ) {
            // Add account user email  to existing recipient
            $recipient .= ','.$wp_user->user_email;
        }
    }

    return $recipient;
}

Code goes in function.php file of your active child theme (active theme). It should works.


2) Add the customer account email as a CC email address:

add_filter( 'woocommerce_email_headers', 'add_cc_email_to_headers', 10, 3);
function add_cc_email_to_headers($header, $email_id, $order) {
    // Only for "Customer Processing Emails"  email notifications
    if( 'customer_processing_order' == $email_id ) {

        if( $order->get_customer_id() > 0 ){
            // Get the customer WP_User object
            $wp_user = new WP_User($order->get_customer_id());

            if ( $wp_user->user_email != $order->get_billing_email() ) {
                $header .= 'Cc: ' . utf8_decode($order->get_formatted_billing_full_name() . ' <' . $wp_user->user_email . '>') . "\r\n";
            }
        }
    }
    return $header;
}

Code goes in function.php file of your active child theme (active theme). It should works.

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