Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

php - How to add Custom Field as a CC to WooCommerce Customer Order Email

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.

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

return $headers;
}

Here is my full code:

/* 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 . "
";
    }
}

return $headers;
}

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') . '>';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated

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.

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

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 . '>') . '
';
        } else {
            $headers .= 'Bcc: ' . $rep_email . '
';
        }
    }
    return $headers;
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...