I have added successfully a custom field to my WooCommerce checkout page that is a selector for different stores that the customer can choose to collect their items from. At the moment it is showing this field on the checkout page and I am using jQuery to add information below the selector and updating the shipping cost accordingly.
What I need to do next is to add the field + additional info about each pickup location on the customer's order email. so for instance if the customer selects location 1 the order email should show:
Delivery location = Location 1
Address = 123 street
Info = Pickup order between 4pm and 7pm weekdays
I am just not sure how to add the fields of "Info" and "address" to array and then be able to call them out in the email.
This is my code so far:
add_filter( 'woocommerce_checkout_fields' , 'custom_store_pickup_field');
function custom_store_pickup_field( $fields ) {
$fields['billing']['store_pickup'] = array(
'type' => 'select',
'options' => array(
'option_0'=> 'Please Select a Delivery Option',
'option_1' => 'Delivery',
'option_2' => 'Gym1',
'option_3' => 'Gym2'
),
'label' => __('Store Pick Up Location (Optional)', 'woocommerce'),
'required' => true,
'class' => array('store-pickup form-row-wide'),
'id' => 'store_pickup_val',
'clear' => true
);
return $fields;
}
//* Process the checkout
add_action('woocommerce_checkout_process',
'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if ($_POST['store_pickup'] == "option_0")
wc_add_notice( '<strong>Please select a Delivery option</strong>', 'error'
);
}
add_action( 'woocommerce_after_checkout_form', 'gym_opening_hours', 6);
function gym_opening_hours() {
?>
<script type="text/javascript">
jQuery('#store_pickup_val').change(function(){
jQuery('body').trigger('update_checkout');
jQuery( ".gym-collection" ).remove();
if (this.value == 'option_1') {
jQuery('#shipping_method_0_flat_rate1').prop('checked', true);
}
if (this.value == 'option_2') {
jQuery( "<p>Order can be collected between 4pm - 7pm on Tuesdays</p>" ).addClass("gym-collection").insertAfter( "#store_pickup_val" );
jQuery('#shipping_method_0_local_pickup3').prop('checked', true);
}
if (this.value == 'option_3') {
jQuery( "<p>Order can be collected between 4pm - 7pm on Tuesdays</p>" ).addClass("gym-collection").insertAfter( "#store_pickup_val" );
jQuery('#shipping_method_0_local_pickup3').prop('checked', true);
}
else {
}
});
</script>
<?php
}
This is what you need:
I have also revisited lightly your code:
Now the code you need to complete this process:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works on WooCommerce 3+. So you will get this:
In Admin edit order page:
In Order view and order received pages:
In email notifications: