[英]Add customer email address to new account email notification in WooCommerce
[英]Add account email conditionally on Customer processing Order email notification in Woocommerce
所以这更多是我的解决方案,但我想打开它,看看社区是否可以找到更好的替代方案,或者是否有可能找到此解决方案的用途。
我们的客户要求我们对创建订单时收到的电子邮件订单收据进行以下更改:收据应发送给帐户持有人,如果不同,请抄送账单电子邮件
正如我们所知,Woocommerce 默认仅根据结帐时设置的billing_email发送订单收据(客户处理),因此我开始寻找一种方法来添加帐户所有者电子邮件的附加信息。
我做了一些挖掘并在 Stackoverflow 上找到了一些关于如何做到这一点的答案,并且提议的解决方案利用了woocommerce_email_recipient_customer_processing_order内置函数。 这种方法只会在“to”标题中添加电子邮件 - 不太理想。 它也没有考虑到潜在的重复发送到同一电子邮件地址,至少在我们这里的服务器的情况下,导致电子邮件无法发送。 没有布埃诺。
下面的函数代表了一个解决这个问题的方法,其中我们调用 WP Core 函数wp_get_current_user()来获取用户关联的电子邮件,然后检查它是否与计费电子邮件相同。
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;
}
该逻辑打算按以下方式流动:
据我所知,没有更简单的方法来处理这个问题,所以我把它贴在这里,希望看看其他人是否有更优雅的解决方案。 上面的代码通过放置在子主题functions.php 中来工作。
您无法在电子邮件通知挂钩上获取当前用户或当前用户 ID。
您首先需要从订单中获取客户 ID ,然后您可以获取WP_User
对象以获取帐户电子邮件。
当与客户处理订单电子邮件通知中的订单账单电子邮件不同时,有两种不同的方法可以添加客户帐户电子邮件:
1) 将客户帐户电子邮件添加为附加收件人:
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 文件中。 它应该有效。
2) 将客户账户邮箱添加为抄送邮箱:
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 文件中。 它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.