【问题标题】:Add custom user email to CC for specific Woocommerce email notification将自定义用户电子邮件添加到 CC 以获取特定 Woocommerce 电子邮件通知
【发布时间】:2018-09-21 18:38:52
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 您在此处缺少 $user_id。问题是:您想使用谁的用户元数据“order_cc_email”?客户的,管理员的?
  • 是的,谢谢 - 如何添加 $user_id?我已经尝试了许多在以前的帖子中找到的不同代码(包括这个:stackoverflow.com/questions/43814132/…)但没有成功,我不明白它是如何工作的。该电子邮件是他们在注册帐户时指明的会计电子邮件(这是一个 B2B 网站),因此该电子邮件仅作为保存在用户页面中的自定义元数据提供

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


【解决方案1】:

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

  • $header ===> 要在此过滤器中返回的标头数据
  • $email_id==> 当前的 WC_Email ID (但不是 $object...)
  • $order ====> 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 文件中。经过测试和工作。

相关话题:

【讨论】:

    【解决方案2】:

    我已经修改了上面的代码以包含第二个 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;
    }
    

    我确实有几个问题,为什么下面的 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',
      );
    

    我曾尝试将这些想法融入现有代码中,但未能使其发挥作用。

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2019-04-25
      • 1970-01-01
      • 2018-01-07
      • 2021-12-11
      • 2018-09-14
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多