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

♀尐吖头ヾ 提交于 2020-01-03 03:21:06

问题


So this is more of my solution here but I wanted to open this up to see if the community could find a better alternative or potentially find use for this solve.

Our client asked us for the following alteration to the email order receipts received on order creation: The receipts should go to the account holder and cc the billing email if it's different

As we know Woocommerce by default sends the order receipt (Customer Processing) based on only the set billing_email on checkout so I started to look for a way to add on the tack on the account owner email to it as well.

I did some digging and found a few answers on Stackoverflow on how to do this and the proposed solution utilized the woocommerce_email_recipient_customer_processing_order built-in function. This approach would only add on the email in the "to" header -less than ideal. It also doesn't account for potential duplicate sends to the same email address which, at least in the case of our server here, caused the email to not be delivered. No bueno.

The function below represents a work around this wherein we're calling upon the WP Core function wp_get_current_user() to obtain the email that the user is associated with and then checking whether it's the same as the billing email.

add_filter( 'woocommerce_email_headers', 'add_email_recipient', 10, 3);

function add_email_recipient($header, $email_id, $order) {
    // Only for "Customer Processing Emails"  email notifications
    if( ! ( 'customer_processing_order' == $email_id ) ) return;
    $curr_user = wp_get_current_user();
    $account_holder_email = $curr_user->user_email;
    $billing_email = $order->get_billing_email();
    $header ='';
    if ( $account_holder_email != $billing_email ) {
        $header .= 'Cc: '.$account_holder_email;
    }
    return $header;
}

The logic intends to flow the following way:

  • Adjust the woocommerce email headers
  • If the email is the "customer_processing_order" continue
  • Get the current user email
  • Get the billing email set in the order
  • assign a CC field of the current user email
  • done

As far as I could tell there wasn't an easier way to handle this so I'm posting this up here in the hopes to see if someone else has a more elegant solution. The above code works by placing in the child theme functions.php.


回答1:


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.



来源:https://stackoverflow.com/questions/53212045/add-account-email-conditionally-on-customer-processing-order-email-notification

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