vconfused101 vconfused101 - 2 months ago 16
PHP Question

Adding a custom email recipient depending on selected custom checkout field value

I need for Woocommerce to send a custom email to different individuals depending on the option selected for Field Checkout (technically, the custom field is the person reporting on the product variant they have purchased, but I was not sure how to customize email receipt based on product variant purchased, so it is as follows).

First I established the custom field using the following code

/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>';

woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('wps-drop'),
'label' => __('Membership purchased'),
'options' => array(
'blank' => __( 'Select membership ordered', 'wps' ),
'premium' => __( 'Premium Membership', 'wps' ),
'gold' => __( 'Gold Membership', 'wps' ),
'silver' => __( 'Silver Membership', 'wps' ),
'bronze' => __( 'Bronze Membership', 'wps' )
)
), $checkout->get_value( 'my_field_name' ));

echo '</div>';

}

/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] =='blank')
wc_add_notice( __( 'Please select status.' ), 'error' );
}


Then I setup email receipts based on value selected:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {

// Get the order ID (retro compatible)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

// Get the custom field value (with the right $order_id)
$my_field_name = get_post_meta($order_id, 'my_field_name', true);

if ($my_field_name == "premium")
$recipient .= ', emailreceipt1@gmail.com';
elseif ($my_field_name == "gold")
$recipient .= ', emailreceipt2@gmail.com';
elseif ($my_field_name == "silver")
$recipient .= ', emailreceipt1@gmail.com';
elseif ($my_field_name == "bronze")
$recipient .= ', emailreceipt2@gmail.com';

return $recipient;
}


When I use this code though, none of the receipts who are supposed to receive their designated email actually do. What's wrong with the code?

Answer Source

You have just missed to save the selected custom field value in the order meta data. I have also revisited your code a bit:

// Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'select',
        'class'     => array('wps-drop'),
        'label'     => __('Membership purchased'),
        'required'  => true, // Missing
        'options'   => array(
            ''          => __( 'Select membership ordered', 'wps' ),
            'premium'   => __( 'Premium Membership', 'wps' ),
            'gold'      => __( 'Gold Membership', 'wps' ),
            'silver'    => __( 'Silver Membership', 'wps' ),
            'bronze'    => __( 'Bronze Membership', 'wps' )
        )
    ), $checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

// Process the checkout
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( empty( $_POST['my_field_name'] ) )
        wc_add_notice( __( 'Please select status.' ), 'error' );
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_field_checkout_update_order_meta', 10, 1 );
function my_custom_field_checkout_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}

add_filter( 'woocommerce_email_recipient_new_order',  'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {

    // Get the order ID (retro compatibility)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // Get the custom field value (with the right $order_id)
    $my_field_name = get_post_meta( $order_id, 'my_field_name', true );

    if ($my_field_name == "premium")
        $recipient .= ', emailreceipt1@gmail.com';
    elseif ($my_field_name == "gold")
        $recipient .= ', emailreceipt2@gmail.com';
    elseif ($my_field_name == "silver")
        $recipient .= ', emailreceipt1@gmail.com';
    elseif ($my_field_name == "bronze")
        $recipient .= ', emailreceipt2@gmail.com';

    return $recipient;
}

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

Tested in WooCommerce 3+ and works.

As you will see the recipient is correctly added in "New Order" email notification depending on the customer choice.