I'm trying to add a custom email field in Woocommerce.
Code to add the pickup date field to checkout:
// ----------------------------------------------------------------------------
// Pickup Date Field
// ----------------------------------------------------------------------------
add_action( 'woocommerce_after_order_notes', 'gfm_pickup_date_field' );
function gfm_pickup_date_field( $checkout ) {
// The pickup delay is the minimum number of days between when an order is placed
// and when it can be picked up.
$pickup_delay = gfm_get_min_pickup_delay();
echo '<div id="gfm_pickup_date_field"><h3>' . __('Pickup') . '</h3>';
woocommerce_form_field( 'gfm_pickup_date', array(
'type' => 'text',
'class' => array('gfm_pickup_date-class form-row-wide'),
'label' => __('Pickup Date'),
'placeholder' => __('month/day/year'),
), $checkout->get_value( 'gfm_pickup_date' ));
echo '</div>';
echo "<script type='text/javascript'>
jQuery(document).ready(function() {
jQuery('#gfm_pickup_date').datepicker({
dateFormat : 'mm/dd/yy',
minDate: {$pickup_delay}
});
});
</script>";
}
Code to save pickup date with order:
// ----------------------------------------------------------------------------
// Save Pickup Date with Order
// ----------------------------------------------------------------------------
add_action( 'woocommerce_checkout_update_order_meta', 'gfm_checkout_field_update_order_meta' );
function gfm_checkout_field_update_order_meta( $order_id ) {
// Save pickup date if user chose to pickup the order
if (WC()->session->get('chosen_shipping_methods')[0] == 'local_pickup') {
update_post_meta( $order_id, 'gfm_pickup_date', sanitize_text_field( $_POST['gfm_pickup_date'] ) );
}
}
And code to send the emails:
// ----------------------------------------------------------------------------
// Add the Pickup Date to Emails
// ----------------------------------------------------------------------------
add_filter('woocommerce_email_order_meta_keys', 'gfm_order_meta_keys');
function gfm_order_meta_keys( $keys ) {
$keys[] = 'gfm_pickup_date';
return $keys;
}
function gfm_enqueue_scripts() {
wp_enqueue_style( 'jquery-ui-css', get_template_directory_uri() . '/jquery-ui-1.11.4/jquery-ui.min.css');
wp_enqueue_script( 'jquery-ui-js', get_template_directory_uri() . '/jquery-ui-1.11.4/jquery-ui.min.js', array('jquery'));
}
add_action( 'wp_enqueue_scripts', 'gfm_enqueue_scripts' );