1
votes

I've added a custom field to the checkout page on my Woocommerce store. The field is 'Restaurant Location'. Upon a customer placing an order, my goal is to use the 'Restaurant Location' field to determine which email to send the order confirmation to.

Here's how I defined the custom field.

/////// Hook custom field in ///////

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' );

function custom_checkout_fields( $fields ) {

$fields['order']['restaurant_location'] = array(
'label'       => __('Food options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required'    => true,
'clear'       => false,
'type'        => 'select',
'options'     => array(
    'no' => __('New Orleans', 'woocommerce' ),
    'br' => __('Baton Rouge', 'woocommerce' )
    )
);

 return $fields;

}

Here's my attempt at the email filter.

add_filter( 'woocommerce_email_recipient_new_order', 'gon_conditional_email_recipient', 10, 2 );

function gon_conditional_email_recipient( $recipient, $order ) {

$gon_order_data = $order->get_data();
$gon_restaurant_location = $gon_order_data['order']['restaurant_location'];

if (  $gon_restaurant_location == 'New Orleans' ) {
    $recipient = '[email protected]';
    return $recipient;
} 
else if (  $gon_restaurant_location == 'Baton Rouge' ) {
    $recipient = '[email protected]';
    return $recipient;
} 

return $recipient;


}

The email filter is working, i.e. I can get an email to go to either address, but I can't seem to pull in the '$gon_restaurant_location' variable properly. Any ideas?

Thanks,

pS

1

1 Answers

1
votes

Your custom field value is not saved in database, so that's why is not working. Try this complete solution instead:

// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {

    woocommerce_form_field( 'restaurant_location', array(
        'type'        => 'select',
        'class'       => array('my-field-class form-row-wide'),
        'label'       => __('Food options', 'woocommerce'),
        'required'    => true,
        'options'     => array(
            ''   => __('Please select an option', 'woocommerce' ),
            'New Orleans' => __('New Orleans', 'woocommerce' ),
            'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
        )
    ), $checkout->get_value( 'restaurant_location' ));
}

// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['restaurant_location'] )
        wc_add_notice( __( 'Please select a food option .' ), 'error' );
}

// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['restaurant_location'] ) ) {
        update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
    }
}

// Display field value on the order edit page
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){
    echo '<p><strong>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}

// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    $location = get_post_meta( $order->get_id(), '_restaurant_location', true );
    $recipient = $location == 'New Orleans' ? ',[email protected]' : ',[email protected]';
    return $recipient;
}

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

This code is tested on Woocommerce 3+ and works


Based on official developer documentation: Adding a custom special field