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?
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 ) {
if( is_admin() ) return $recipient;
// Get the order ID (Woocommerce 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.
Related
I have successfully added the phone number field in Ship to different address and the phone number is showing in back-end as well. However I am not receiving the phone number in email.
Kindly Help
This code helps me to add field:
add_filter( 'woocommerce_checkout_fields', 'bbloomer_shipping_phone_checkout' );
function bbloomer_shipping_phone_checkout( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => 'Phone',
'required' => true,
'class' => array( 'form-row-wide' ),
);
return $fields;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'bbloomer_shipping_phone_checkout_display' );
function bbloomer_shipping_phone_checkout_display( $order ){
echo '<p><b>Shipping Phone:</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
I have tried adding additional code in the above code (shown below) to show the phone number in email. Still doesn't work!
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Shipping Phone' ),
'value' => get_post_meta( $order->id, '_shipping_phone', true ),
);
return $fields;
}
You're missing the correct meta_key, I have also slightly modified your existing code.
Note: by adjusting the priority argument, you can set your field in the correct location.
So you get:
// Shipping field on my account edit-addresses and checkout
function filter_woocommerce_shipping_fields( $fields ) {
$fields['shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide' ),
'priority' => 55
);
return $fields;
}
add_filter( 'woocommerce_shipping_fields' , 'filter_woocommerce_shipping_fields', 10, 1 );
// Display on the order edit page (backend)
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
if ( $value = $order->get_meta( '_shipping_phone' ) ) {
echo '<p><strong>' . __( 'Shipping Phone', 'woocommerce' ) . ':</strong> ' . $value . '</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
// Display on email notifications
function filter_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
// Get meta
$shipping_phone = $order->get_meta( '_shipping_phone' );
// NOT empty
if ( ! empty( $shipping_phone ) ) {
$fields['_shipping_phone'] = array(
'label' => __( 'Shipping Phone', 'woocommerce' ),
'value' => $shipping_phone,
);
}
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'filter_woocommerce_email_order_meta_fields', 10, 3 );
I would appreciate any assistance with the following...
On my WooCommerce checkout page, I need to allow my customers to select multiple distributors and then have the new order emails sent out to myself and only the distributors they selected.
I can't for the life of me figure out how to set it up using multi-checkboxes, which would allow them to select more than one (i.e. they select Distributor 1 and 3, and only the three of us receive the new order email).
I was able to figure it out using a dropdown menu (thanks to reading through a variety of posts within this community), however, this option only allows for one selection. Any help would be greatly appreciated! :)
Current code I was able to piece together:
// // Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'sj_custom_checkout_field' );
function sj_custom_checkout_field( $checkout ) {
echo '<div id="sj_custom_checkout_field"><h2>' . __('Distributor') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('wps-drop'),
'required' => true, // Missing
'options' => array(
'' => __( 'Select Your Preferred Distributor', 'wps' ),
'Distributor 1' => __( 'Distributor 1', 'wps' ),
'Distributor 2' => __( 'Distributor 2', 'wps' ),
'Distributor 3' => __( 'Distributor 3', 'wps' ),
'Distributor 4' => __( 'Distributor 4', 'wps' ),
'Distributor 5' => __( 'Distributor 5', 'wps' )
)
), $checkout->get_value( 'my_field_name' ) );
echo '</div>';
}
// Process the checkout
add_action('woocommerce_checkout_process', 'sj_custom_checkout_field_process');
function sj_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 your preferred distributor.' ), 'error' );
}
// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'sj_custom_field_checkout_update_order_meta', 10, 1 );
function sj_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 ) {
if( is_admin() ) return $recipient;
// Get the order ID (Woocommerce 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 == "Distributor 1")
$recipient .= ',1#email.com';
elseif ($my_field_name == "Distributor 2")
$recipient .= ',2#email.com';
elseif ($my_field_name == "Distributor 3")
$recipient .= ',3#email.com';
elseif ($my_field_name == "Distributor 4")
$recipient .= ',4#email.com';
elseif ($my_field_name == "Distributor 5")
$recipient .= ',5#email.com';
return $recipient;
}
I am trying to get some custom fields attached to my order in Woocommerce.
I have succeeded previously in getting some new fields entered by the user in the checkout based on this official documentation.
What I am trying to achieve is passing a set string value to a custom field based on some other product checking logic I have working. Basically if product of a certain category:
is in cart, display message one: (shipping_instructions_delivery_field_update_order_meta)
otherwise display message two (shipping_instructions_pickup_field_update_order_meta).
My code:
// adds order note at checkout page for the RRNC to pickup at home
add_action( 'woocommerce_before_checkout_form', 'specific_checkout_content', 12 );
function specific_checkout_content() {
// set your special category name, slug or ID here:
$special_cat = 'randwick-rugby-netball-club';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat, 'product_cat', $item->id ) )
$bool = true;
}
// If the special category is detected in one items of the cart
// It displays the message
if ($bool)
{
echo '<div class="checkout-instructions"><p><strong>PLEASE PICK UP ORDER FROM THE CLUB.</strong></p></div>';
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false'); //Removes the ship to different address for club store items
add_filter( 'woocommerce_enable_order_notes_field', 'remove_wc_order_notes' ); //Removes the order notes
add_action( 'woocommerce_checkout_update_order_meta', 'shipping_instructions_pickup_field_update_order_meta' ); //Adds the pick up note for the pdf slip
add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process_pickup' );
}
else
{
echo '<p><strong>Orders are dispatched within 2 business days and shipping times are estimated at between 3-7 business days depending on your location within Australia.</strong></p>';
add_action( 'woocommerce_before_order_notes', 'delivery_instructions_field', 10 ); //Adds the Delivery Instructions fields to the checkout
add_action( 'woocommerce_checkout_update_order_meta', 'shipping_instructions_delivery_field_update_order_meta' ); //Adds the delivery note for the pdf slip
add_action( 'woocommerce_checkout_process', 'my_custom_checkout_field_process_delivery' );
}
}
function my_custom_checkout_field_process_delivery( ) {
global $woocommerce;
// Check if set, if its not set add an error.
if ( !$_POST[ 'delivery_instructions' ] )
wc_add_notice( __( 'Please select from the delivery options.' ), 'error' );
/*$woocommerce->add_error( __( 'Please select from the delivery options.' ) );*/
if ( !$_POST[ 'sport_instructions' ] )
wc_add_notice( __( 'Please select the sport of the order' ), 'error' );
/*$woocommerce->add_error( __( 'Please select the sport of the order' ) );*/
}
function my_custom_checkout_field_process_pickup( ) {
global $woocommerce;
// Check if set, if its not set add an error.
if ( !$_POST[ 'sport_instructions' ] )
wc_add_notice( __( 'Please select the sport of the order' ), 'error' );
/*$woocommerce->add_error( __( 'Please select the sport of the order' ) );*/
}
// remove Order Notes from checkout field in Woocommerce
function remove_wc_order_notes() {
return false;
}
function shipping_instructions_delivery_field_update_order_meta( $order_id ) {
update_post_meta( $order_id, 'shipping_instructions', 'IF YOU HAVE NOT RECEIVED YOUR ORDER WITHIN 7 DAYS, PLEASE CONTACT US TO FOLLOW UP' );
}
function shipping_instructions_pickup_field_update_order_meta( $order_id ) {
update_post_meta( $order_id, 'shipping_instructions', 'PLEASE PICK UP FROM THE CLUB SHOP' );
}
//Admin side
/**
* Update the order meta with field value
**/
add_action( 'woocommerce_checkout_update_order_meta', 'delivery_instructions_field_update_order_meta' );
function delivery_instructions_field_update_order_meta( $order_id ) {
if ( $_POST[ 'delivery_instructions' ] )
update_post_meta( $order_id, 'delivery_instructions', esc_attr( $_POST[ 'delivery_instructions' ] ) );
}
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_shipping_fields_display_admin_order_meta', 10, 1);
function my_custom_shipping_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Shipping Note') . ':</strong><br> ' . get_post_meta($order->id, 'shipping_instructions', true) . '</p>';
}
$shipping_instructions = get_post_meta($wpo_wcpdf->export->order->id,'shipping_instructions',true);
if (isset($shipping_instructions)) {
echo $shipping_instructions;
}
/**
* Add the delivery instructions field to the checkout
**/
function delivery_instructions_field( $checkout ) {
echo '<div id="delivery_instructions_field"><h2>' . __('Delivery Instructions') . '</h2>';
woocommerce_form_field_radio( 'delivery_instructions', array(
'type' => 'select',
'class' => array(
'delivery_instructions form-row-wide'
),
'label' => __( '' ),
'placeholder' => __( '' ),
'required' => true,
'options' => array(
'Signature Required' => '<b> Signature Required</b><br/>A signature is required for the goods to be left at the intended address. If there is no one to sign for your delivery, a card will be left and it is then your responsibility to collect the items from the nearest depot or arrange for the courier to return. This will be at your expense<br/><br/>',
'Leave Unattended' => '<b> Authority to leave unattended</b><br/>Courier will leave your goods at the intended address. You can give directions in the “Order Notes” below for the best place to leave your delivery',
)
), $checkout->get_value( 'delivery_instructions' ) );
echo '</div>';
}
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);
function my_custom_billing_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Delivery Instructions') . ':</strong><br> ' . get_post_meta($order->id, 'delivery_instructions', true) . '</p>';
}
$delivery_instructions = get_post_meta($wpo_wcpdf->export->order->id,'_delivery_instructions',true);
if (isset($delivery_instructions)) {
echo $delivery_instructions;
}
/**
* Add the field to the checkout to get the sport of what the customer orders
**/
add_action( 'woocommerce_after_order_notes', 'sport_instructions_field', 20 );
function sport_instructions_field( $checkout ) {
echo '<div id="sport_instructions_field"><h2>' . __('Which sport is this order in relation to?') . '</h2>';
woocommerce_form_field_radio( 'sport_instructions', array(
'type' => 'select',
'class' => array(
'sport_instructions form-row-wide'
),
'label' => __( '' ),
'placeholder' => __( '' ),
'required' => true,
'options' => array(
'Rugby Union' => '<b>Rugby Union</b><br/>',
'Rugby League' => '<b>Rugby League</b><br/>',
'Netball' => '<b>Netball</b><br/>',
'Football' => '<b>Football</b><br/>',
'AFL' => '<b>AFL</b><br/>',
'Touch Tag' => '<b>Touch & Tag</b>',
)
), $checkout->get_value( 'sport_instructions' ) );
echo '</div>';
}
/**
* Update the order meta with field value
**/
add_action( 'woocommerce_checkout_update_order_meta', 'sport_instructions_field_update_order_meta' );
function sport_instructions_field_update_order_meta( $order_id ) {
if ( $_POST[ 'sport_instructions' ] )
update_post_meta( $order_id, 'sport_instructions', esc_attr( $_POST[ 'sport_instructions' ] ) );
}
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_sports_fields_display_admin_order_meta', 20, 1);
function my_custom_sports_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Sport in relation to') . ':</strong><br> ' . get_post_meta($order->id, 'sport_instructions', true) . '</p>';
}
$sport_instructions = get_post_meta($wpo_wcpdf->export->order->id,'_sport_instructions',true);
if (isset($sport_instructions)) {
echo $sport_instructions;
}
I would think that the update_post_meta() with a string at the end should set it but it does not appear to do anything. Or perhaps I don't have the proper fuctionality with:
function my_custom_checkout_field($checkout).
Any help is appreciated.
Try this code. _shipping_instructions replace this with shipping_instructions
$shipping_instructions = get_post_meta($wpo_wcpdf->export->order >id,'shipping_instructions',true);
if (isset($shipping_instructions)) {
echo $shipping_instructions;
}
I found this code on here earlier today and I tried modifying it but I get an error code when I try to add more emails to the code (it starts with 2) not sure what I'm doing wrong.
Here is the example
This is my modified Code (tried adding a third email to the bottom and I'm getting a php error)
// 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' => __('Select Location', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'4289 boul St-Jean' => __('4289 boul St-Jean', 'woocommerce' ),
'3559 boul St-Charles' => __('3559 boul St-Charles', '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 ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
return $recipient;
}
This is the original Code I found
// 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 ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == 'New Orleans' ? ',test1#example.com' : ',test1#example.com';
return $recipient;
}
Change this line :
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
to :
switch ($location) {
case '4289 boul St-Jean':
$recipient = ',nicks#mtygroup.com';
break;
case '3559 boul St-Charles':
$recipient = ',nicsoti#yahoo.com';
break;
default:
$recipient = ',nicks#mtygroup.com';
break;
}
Or just modify it to suits your needs
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 = 'test1#gmail.com';
return $recipient;
}
else if ( $gon_restaurant_location == 'Baton Rouge' ) {
$recipient = 'test2#gmail.com';
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
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' ? ',test1#example.com' : ',test1#example.com';
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