Skip to content
Advertisement

Time loop for WooCommerce checkout select option from date picker input field

Input field one – delivery date has a datepicker attached. This fills in the required date by client. This field determines the delivery time options that are populated in the select field next to it.

However the options are based on some rules

  1. Time slots are 30 mins intervals from 11am to 4pm.
  2. If the date chosen = today, delivery start time = current time + one hour.
  3. if date is future, delivery is from 11am to 4pm. 30 mins intervals

I need help making the array for the woocommerce options basing on the parameters above from the chosen date. Thanks.

Screenshot is descriptive.

Sample code: First I update the select field options on selecting the date in the using the date picker. Using the code below.

JavaScript

Then using the date chosen from date-picker, I try to make the array to send to the select field in the form.

JavaScript

Advertisement

Answer

As the code for the Delivery date was missing, I have made it enabling jQuery-ui datepicker (and you should use your own CSS).

I haven’t use your jQuery code, because my approach is quiet different and I was a bit loosed in your code. My PHP code is also quiet different. The select field <options> is almost empty in php, because javascript build it dynamically.

Here php code is passing to Javascript/jQuery some data:

  • for the delivery starting date.
  • for the select option key/value pairs (available time slots + default time slots).

Here jQuery/Javascript is located in the same hooked function just after the custom checkout fields code…

The html for <option> tags in the “delivery time select field are built dynamically by jQuery depending on the Start date and the current time.

All your requirement are respected:

  • Time slots are 30 mins intervals from 11am to 4pm.
  • If selected date is today we add an offset of +1 hour to the time slots
  • If selected date is not today, we got the default time slots (11 available)

Now you should disable your related code to try this one:

JavaScript

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

Tested and works.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement