如何将自定义字段作为抄送添加到 WooCommerce 客户订单电子邮件 [英] How to add Custom Field as a CC to WooCommerce Customer Order Email

查看:31
本文介绍了如何将自定义字段作为抄送添加到 WooCommerce 客户订单电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WooCommerce 结帐页面中设置了一些自定义字段.这些字段是名称和电子邮件字段.我想将电子邮件作为抄送添加到 customer_on_hold_order 电子邮件中.

I've setup some custom fields in the WooCommerce checkout page. These fields are a name and an email field. I want to add the email as a CC to the customer_on_hold_order email.

我已经能够使用此代码做到这一点,但它向抄送地址发送了 2 封电子邮件.我的代码做错了什么?

I have been able to do that with this code but it is sending 2 emails to the CC address. What am I doing wrong in my code?

/* Send email to Rep Email */
add_filter( 'woocommerce_email_headers', 'send_email_to_gss_rep', 10, 3);
function send_email_to_gss_rep( $headers, $email_id, $order ) {
if ($email_id == 'customer_on_hold_order') {
    $custom_rep_email = $order->get_meta( 'Rep Email', true );
    if ( $custom_rep_email ) {
        $headers .= 'CC: ' . $custom_rep_email . "\r\n";
    }
}

return $headers;
}

这是我的完整代码:

/* Add custom fields to Woo checkout */
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_fields' );

function my_custom_checkout_fields( $checkout ) {

    woocommerce_form_field( 'rep_name', array(
      'type'  => 'text',
      'class' => array( 'rep_name' ),
      'label' => __( 'Rep Name' ),
    ), $checkout->get_value( 'rep_name' ) );

    woocommerce_form_field( 'rep_email', array(
      'type'  => 'email',
      'class' => array( 'rep-email' ),
      'label' => __( 'Rep Email' ),
    ), $checkout->get_value( 'rep_email' ) );

}

/* Update the order meta with custom field values */
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset($_POST['rep_name']) && ! empty($_POST['rep_name']) ) {
        $order->update_meta_data( 'Rep Name', sanitize_text_field( $_POST['rep_name'] ) );
    }
    if ( isset($_POST['rep_email']) && ! empty($_POST['rep_email']) ) {
        $order->update_meta_data( 'Rep Email', sanitize_text_field( $_POST['rep_email'] ) );
    }
}

/* Display the custom field value on admin order pages after billing address */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
  if ( ! empty($order->get_meta('Rep Name')) ){
    echo '<p><strong>'.__('Rep Name').':</strong> ' . $order->get_meta('Rep Name') . '</p>';
  }
  if ( ! empty($order->get_meta('Rep Email')) ){
    echo '<p><strong>'.__('Rep Email').':</strong> ' . $order->get_meta('Rep Email') . '</p>';
  }
}

/* Display the custom field value on email notifications */
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
  if ( ! empty($order->get_meta('Rep Name')) ){
    echo '<p><strong>'.__('Rep Name').':</strong> ' . $order->get_meta('Rep Name') . '</p>';
  }
  if ( ! empty($order->get_meta('Rep Email')) ){
    echo '<p><strong>'.__('Rep Email').':</strong> ' . $order->get_meta('Rep Email') . '</p>';
  }
}

/* Send email to Rep Email */
add_filter( 'woocommerce_email_headers', 'send_email_to_gss_rep', 10, 3);
function send_email_to_gss_rep( $headers, $email_id, $order ) {
if ($email_id == 'customer_on_hold_order') {
    $gss_email = $order->get_meta( 'Rep Email', true );
    if ( $gss_email ) {
        $headers .= 'CC: ' . $alternative_email . "\r\n";
    }
}

return $headers;
}

另外,我想要像 John Doe 这样的抄送电子邮件,但是当我使用以下内容时我无法让它工作:

Also, I would like to have the CC email like John Doe but I cannot get that to work when I use something like:

$custom_rep_email = $order->get_meta('Rep Name') . '<' . $order->get_meta('Rep Name') . '>';

推荐答案

更新

在定位客户通知时,在电子邮件标头上使用 woocommerce_email_headers 过滤器钩子和 CcBcc 时似乎存在错误.一封电子邮件被发送两次.我在 WooCommerce Github 上打开了一个问题.

It seems that there is a bug when using woocommerce_email_headers filter hook with Cc or Bcc on email headers when targeting customer notifications. An email is sent twice. I have opened an issue on WooCommerce Github.

但如果您的目标 new_order 电子邮件 ID 不会发生.

But if your target new_order Email ID it doesn't happen.

现在要拥有用户名的 CC,您需要使用以下内容:

Now to have the CC with the user name, you need to use something like:

add_filter( 'woocommerce_email_headers', 'additional_cc_recipient', 10, 3 );
function additional_cc_recipient( $headers, $email_id, $order ) {
    if ( $email_id === 'customer_on_hold_order' && ( $rep_email = $order->get_meta('Rep Email') ) ) {
        if ( $rep_name = $order->get_meta('Rep Name') ) {
            $headers .= 'Bcc: ' . utf8_decode($rep_name . ' <' . $rep_email . '>') . '\r\n';
        } else {
            $headers .= 'Bcc: ' . $rep_email . '\r\n';
        }
    }
    return $headers;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of your active child theme (or active theme).

这篇关于如何将自定义字段作为抄送添加到 WooCommerce 客户订单电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆