1
votes

on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.

I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?

Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?

Thank you in advance!

My site: www.monpetitfour.com

2

2 Answers

1
votes

You should try to had something like that :

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // You can make your own control here
    if ( ! $_POST[ 'e_deliverydate' ] )
        wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}

For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :

 <?php  $delivery_date = get_post_meta( $order->id, 'custom_field_date', true); 
 // If the plugin is well developed, you can't directly use magic getters : 
 // $delivery_date = $order->e_deliverydate;
 // Can only by use if the post meta start with _
?>
 Your delivery date is <?php echo $delivery_date ?>

You can also use

date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );

In order to format the date correctly.

On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).

0
votes

Add the following code to your theme's functions.php file

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['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
} 

Now to get the email to show the custom field,

add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys['Delivery Date'] = '_e_deliverydate';
    return $keys;
}

EDIT : Seems the field value isn't being saved to the database, try saving it explicitly

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['e_deliverydate'] ) ) {
        update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
    }
}