簡體   English   中英

如何將自定義字段作為抄送添加到 WooCommerce 客戶訂單電子郵件

[英]How to add Custom Field as a CC to WooCommerce Customer Order Email

我在 WooCommerce 結帳頁面中設置了一些自定義字段。 這些字段是姓名和電子郵件字段。 我想將電子郵件作為抄送添加到 customer_on_hold_order 電子郵件中。

我已經能夠用這段代碼做到這一點,但它正在向 CC 地址發送 2 封電子郵件。 我在我的代碼中做錯了什么?

/* 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 這樣的 CC 電子郵件,但是當我使用類似以下內容時我無法讓它工作:

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

更新

在針對客戶通知時,在電子郵件標頭上使用帶有CcBccwoocommerce_email_headers過濾器掛鈎時似乎存在錯誤。 一封電子郵件被發送了兩次。 我在 WooCommerce Github 上開了一個問題

但是,如果您的目標new_order電子郵件 ID 不會發生。

現在要讓 CC 具有用戶名,您需要使用類似以下內容的內容:

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 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM