在Woocommerce中有条件地根据客户处理来添加帐户电子邮件通知订单电子邮件通知 [英] Add account email conditionally on Customer processing Order email notification in Woocommerce

查看:89
本文介绍了在Woocommerce中有条件地根据客户处理来添加帐户电子邮件通知订单电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这更多是我的解决方案,但我想对此进行开放,以查看社区是否可以找到更好的替代方案或潜在地找到解决方案的用途.

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

我们知道Woocommerce默认在结帐时仅根据设置的 billing_email 发送订单收据(客户处理),因此我开始寻找一种在帐户所有者电子邮件中添加附加费的方法以及它.

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.

我做了一些挖掘工作,并在Stackoverflow上找到了有关如何执行此操作的答案,并且所提出的解决方案利用了 woocommerce_email_recipient_customer_processing_order 内置函数.这种方法只会在收件人"标头中添加电子邮件-不够理想.它还不考虑可能重复发送到同一电子邮件地址,至少对于我们的服务器而言,这会导致电子邮件无法发送.没有布宜诺斯艾利斯.

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.

下面的函数代表解决此问题的方法,其中我们调用WP Core函数 wp_get_current_user()来获取与用户关联的电子邮件,然后检查其是否与帐单电子邮件.

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:

  • 调整woocommerce电子邮件标题
  • 如果电子邮件是"customer_processing_order",请继续
  • 获取当前用户的电子邮件
  • 获取订单中设置的帐单电子邮件
  • 分配当前用户电子邮件的抄送字段
  • 完成

据我所知,没有比这更简单的方法了,所以我将其发布在这里,希望看看其他人是否有一个更优雅的解决方案.上面的代码通过放置子主题 functions.php 来工作.

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.

推荐答案

您无法在电子邮件通知挂钩上获取当前用户或当前用户ID.
您首先需要从订单中获取客户ID ,然后您可以获取WP_User对象以获取帐户电子邮件.

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)将客户帐户电子邮件添加为其他收件人:

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;
}

代码进入您的活动子主题(活动主题)的function.php文件中.应该可以.

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

2)将客户帐户电子邮件添加为抄送电子邮件地址:

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;
}

代码进入您的活动子主题(活动主题)的function.php文件中.应该可以.

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

这篇关于在Woocommerce中有条件地根据客户处理来添加帐户电子邮件通知订单电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆