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