Add custom user email to CC for specific Woocommerce email notification(将自定义用户电子邮件添加到 CC 以获取特定的 Woocommerce 电子邮件通知)
问题描述
在 Woocommerce 中,我尝试自定义代码 从此线程在客户完成的订单电子邮件通知中添加自定义电子邮件作为抄送"电子邮件地址:
In Woocommerce I try to customize the code from this thread to add a custom email as "CC" email address in customer completed order email notification:
/**
* Function adds a BCC header to emails that match our array
*
* @param string $headers The default headers being used
* @param string $object The email type/object that is being processed
*/
function add_cc_to_certain_emails( $headers, $object ) {
// email types/objects to add cc to
$cc_email = get_user_meta( $user_id, 'order_cc_email', true ); // MY CUSTOM CODE
$add_cc_to = array(
'customer_completed_order', // Customer Processing order from WooCommerce
);
// if our email object is in our array
if ( in_array( $object, $add_cc_to ) ) {
// change our headers
$headers = array(
$headers,
// 'Cc: Me <me@example.com>' ."
", // INITIAL CODE
'Cc: '.$cc_email.' <'.$cc_email.'>' ."
", // MY CUSTOM CODE
}
return $headers;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_certain_emails', 10, 2 );
我找不到从用户元数据中获取自定义用户电子邮件的方法,因此我的代码无法按预期工作.
I can't find the way to get custom user email from user meta data, and so my code doesn't work as expected.
如何从用户元数据中获取自定义用户电子邮件?
How to get the custom user email from user meta data?
如何在电子邮件标题中将此电子邮件(带有客户全名)添加为CC"?
How to add this email (with the customer full name) as "CC" in email header?
推荐答案
您的钩子函数中缺少一些参数,因为 woocommerce_email_headers
过滤器钩子允许 3 个参数:
There is some missing arguments in your hooked function, as woocommerce_email_headers
filter hook allow 3 arguments:
$header
===> 过滤器返回的头部数据$email_id
==> 当前WC_Email
ID (但不是$object
...)莉>$order
====>WC_Order
对象的实例(缺少的有用的)
$header
===> the header data to be return in this filter$email_id
==> the currentWC_Email
ID (but not the$object
…)$order
====> the instance of theWC_Order
object (the missing useful one)
试试这个重新访问的代码:
Try this revisited code instead:
add_filter( 'woocommerce_email_headers', 'custom_cc_email_headers', 10, 3 );
function custom_cc_email_headers( $header, $email_id, $order ) {
// Only for "Customer Completed Order" email notification
if( 'customer_completed_order' !== $email_id )
return $header;
// Get the custom email from user meta data (with the correct User ID)
$custom_user_email = get_user_meta( $order->get_user_id(), 'order_cc_email', true );
if( ! empty($custom_email) )
return $header; // Exit (if empty value)
// Get customer billing full name
$user_name = $order->get_billing_first_name().' ';
$user_name .= $order->get_billing_last_name();
// Merge and prepare the data
$formatted_email = utf8_decode($user_name . ' <' . $custom_user_email . '>');
// Add Cc to headers
$header .= 'Cc: '.$formatted_email .'
';
return $header;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.
Code goes in function.php file of your active child theme (or active theme). Tested and work.
相关主题:
- 从以下位置获取客户 IDWooCommerce 中的订单 ID
- 将自定义电子邮件添加到 BCC对于特定的 Woocommerce 电子邮件通知
这篇关于将自定义用户电子邮件添加到 CC 以获取特定的 Woocommerce 电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将自定义用户电子邮件添加到 CC 以获取特定的
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01