Php 将自定义用户电子邮件添加到CC以获得特定的电子商务电子邮件通知

Php 将自定义用户电子邮件添加到CC以获得特定的电子商务电子邮件通知,php,woocommerce,hook-woocommerce,email-notifications,cc,Php,Woocommerce,Hook Woocommerce,Email Notifications,Cc,在Woocommerce中,我尝试自定义代码,以便在客户已完成订单电子邮件通知中添加自定义电子邮件作为“抄送”电子邮件地址: /** * 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

在Woocommerce中,我尝试自定义代码,以便在客户已完成订单电子邮件通知中添加自定义电子邮件作为“抄送”电子邮件地址:

/**
 * 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>' ."\r\n", // INITIAL CODE
            'Cc: '.$cc_email.' <'.$cc_email.'>' ."\r\n", // MY CUSTOM CODE
    }
    return $headers;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_certain_emails', 10, 2 );
/**
*函数将密件抄送标头添加到与阵列匹配的电子邮件中
* 
*@param string$headers正在使用的默认头
*@param string$object正在处理的电子邮件类型/对象
*/
函数add_cc_to_某些电子邮件($headers,$object){
//要将抄送添加到的电子邮件类型/对象
$cc_email=get_user_meta($user_id,'order_cc_email',true);//我的自定义代码
$add_cc_to=数组(
'客户完成订单',//客户处理来自WooCommerce的订单
);
//如果我们的电子邮件对象在数组中
if(在数组中($object,$add_cc_to)){
//更改标题
$headers=数组(
$headers,
//“Cc:Me.”\r\n“,//初始代码
“Cc:”.$Cc\u email.“。\r\n”,//我的自定义代码
}
返回$headers;
}
添加过滤器('woocommerce\u email\u Header','add\u cc\u to\u某些电子邮件',10,2);
我找不到从用户元数据获取自定义用户电子邮件的方法,因此我的代码无法按预期工作

如何从用户元数据中获取自定义用户电子邮件


如何将此电子邮件(客户全名)添加为电子邮件标题中的“CC”?

钩住的函数中缺少一些参数,因为
woocommerce\u email\u headers
过滤器钩子允许3个参数:

  • $header
    =>要在此筛选器中返回的头数据
  • $email\u id
    ==>当前的
    WC\u email
    id(但不是
    $object
    …)
  • $order
    =>WC\u order对象的实例(缺少的有用对象)
请尝试以下重新访问的代码:

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 .'\r\n';

    return $header;
}
add_filter('woocommerce_email_headers','custom_cc_email_headers',10,3);
函数自定义邮件头($header、$email\u id、$order){
//仅适用于“客户已完成订单”电子邮件通知
如果('customer\u completed\u order'!==$email\u id)
返回$header;
//从用户元数据获取自定义电子邮件(具有正确的用户ID)
$custom\u user\u email=get\u user\u meta($order->get\u user\u id(),'order\u cc\u email',true);
如果(!空($custom_email))
return$header;//退出(如果值为空)
//获取客户帐单全名
$user_name=$order->get_billing_first_name();
$user\u name.=$order->get\u billing\u last\u name();
//合并并准备数据
$formatted_email=utf8_decode($user_name.');
//将Cc添加到标题
$header.='Cc:'.$formatted_email.\r\n';
返回$header;
}
代码放在活动子主题(或活动主题)的function.php文件中。已测试并运行

相关线程:


我修改了上述代码,添加了第二个CC电子邮件地址,该地址可用于所有通知,包括我设置的两封自定义电子邮件,因此感谢您提供原始代码

add_filter( 'woocommerce_email_headers', 'custom_cc_email_headers', 10, 3 );
function custom_cc_email_headers( $headers, $email_id, $order ) {

// Only for "Customer Completed Order" email notification
// if( 'wcj_custom_1' !== $email_id )
//    return $headers;

// Get the custom email from user meta data  (with the correct User ID)
$custom_user_email = get_user_meta( $order->get_user_id(), 'doc_email', true );
$bcc_email = get_user_meta( $order->get_user_id(), 'admin_email', true );

if( ! empty($custom_email) ) 
    return $headers; // 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 . '>');
$formatted_email2 = utf8_decode($user_name . ' <' . $bcc_email . '>');

// Add Cc to headers
$headers .= 'Cc: '.$formatted_email ."\r\n";
$headers .= 'Cc: '.$formatted_email2."\r\n";

return $headers;
}
而不是,在if语句中

$custom_user_email
此外,是否也可以将其放入电子邮件ID数组中,以应用cc地址

// Only for "Customer Completed Order" email notification
// if( 'wcj_custom_1' !== $email_id )
//    return $headers;
像这样的数组(已找到)


我曾尝试将这些想法纳入现有代码中,但没有成功。

您缺少$user\u id。问题是:您想使用谁的用户元数据“order\u cc\u email”?客户的、管理员的?是的,谢谢-如何添加$user\u id?我在以前的帖子中尝试了许多不同的代码(包括这封:)但没有成功,我不知道它是如何工作的。这封电子邮件是他们注册帐户时指明的会计电子邮件(这是一个B-to-B网站),因此该电子邮件只能作为保存在用户页面中的自定义元来使用
// Only for "Customer Completed Order" email notification
// if( 'wcj_custom_1' !== $email_id )
//    return $headers;
$email_ids = array(
   'new_order',
   'cancelled_order',
   'failed_order',
   'customer_on_hold_order',
   'customer_processing_order',
   'customer_completed_order',
   'customer_refunded_order',
   'customer_invoice',
   'customer_note',
   'customer_reset_password',
   'customer_new_account',
  );