向CC添加自定义用户电子邮件以获取特定的Woocommerce电子邮件通知

时间:2018-04-11 18:51:09

标签: php woocommerce hook-woocommerce email-notifications cc

在Woocommerce中,我尝试自定义代码from this thread,在客户完成的订单电子邮件通知中将自定义电子邮件添加为“CC”电子邮件地址:

/**
 * 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 );

我找不到从用户元数据中获取自定义用户电子邮件的方法,因此我的代码无法按预期工作。

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

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

2 个答案:

答案 0 :(得分:0)

你的钩子函数中有一些缺少的参数,因为woocommerce_email_headers过滤器钩子允许3个参数:

  • $header ===&gt;要在此过滤器中返回的标头数据
  • $email_id ==&GT;当前WC_Email ID (但不是$object ...)
  • $order ====&gt; WC_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;
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。

相关主题:

答案 1 :(得分:0)

我已经修改了上面的代码,以包括第二个抄送电子邮件地址,该地址可用于所有通知,包括我设置的几封自定义电子邮件,因此感谢原始代码!

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

我确实有几个问题,为什么下面的var声明为

$custom_email

不是,在if语句中

$custom_user_email

还可以将其设置为电子邮件ID的数组,以将抄送地址应用于吗?

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

就像这样的数组(找到here

$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',
  );

我试图将这些想法合并到现有代码中,但是未能使其起作用。