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
- Time slots are 30 mins intervals from 11am to 4pm.
- If the date chosen = today, delivery start time = current time + one hour.
- 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.
Sample code: First I update the select field options on selecting the date in the using the date picker. Using the code below.
add_action( 'wp_footer', 'woocommerce_update_on_date_select', 21 );
function woocommerce_update_on_date_select() {
if (is_checkout()) {
?>
<script type="text/javascript">
$(document).ready(function() {
function updateCart() {
var d = $("#billing_country").val(),
e = $("#billing_state").val(),
f = $("input#billing_postcode").val(),
g = $("#billing_city").val(),
h = $("input#billing_address_1").val(),
i = $("input#billing_address_2").val(),
j = d,
k = e,
l = f,
m = g,
n = h,
o = i;
$("#ship-to-different-address").find("input").is(":checked") && (j = $("#shipping_country").val(), k = $("#shipping_state").val(), l = $("input#shipping_postcode").val(), m = $("#shipping_city").val(), n = $("input#shipping_address_1").val(), o = $("input#shipping_address_2").val());
var p = {
security: wc_checkout_params.update_order_review_nonce,
payment_method: $("#order_review").find('input[name="payment_method"]:checked').val(),
country: d,
state: e,
postcode: f,
city: g,
address: h,
address_2: i,
s_country: j,
s_state: k,
s_postcode: l,
s_city: m,
s_address: n,
s_address_2: o,
post_data: $("form.checkout").serialize()
};
var c = {update_shipping_method: !0};
if (!1 !== c.update_shipping_method) {
var q = {};
$('select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]').each(function() {
q[$(this).dat$("index")] = $(this).val()
}), p.shipping_method = q
}
$.ajax({
type: "POST",
url: wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%", "update_order_review"),
data: p,
success: function(c) {
var d = $('.woocommerce-checkout input[name="payment_method"]:checked').attr("id");
if ("true" === c.reload) return void window.location.reload();
$(".woocommerce-NoticeGroup-updateOrderReview").remove();
var e = $("#terms").prop("checked");
if (c && c.fragments && $.each(c.fragments, function(b, c) {
$(b).replaceWith(c)
}), e && $("#terms").prop("checked", !0), "failure" === c.result) {
var f = $("form.checkout");
$(".woocommerce-error, .woocommerce-message").remove(), c.messages ? f.prepend('<div class="woocommerce-NoticeGroup-updateOrderReview">' + c.messages + "</div>") : f.prepend(c), f.find(".input-text, select, input:checkbox").blur(), $("html, body").animate({
scrollTop: $("form.checkout").offset().top - 100
}, 1e3)
}
}
});
}
$('#billing_delivery_date').attr('readonly', true);
$('#billing_serving').on('change', function(){
updateCart();
});
$( "#billing_delivery_date" ).on('change', function() {
updateCart();
});
});
</script>
<?php
}
}
Then using the date chosen from date-picker, I try to make the array to send to the select field in the form.
add_filter( 'woocommerce_checkout_fields' , 'brown_remove_billing_postcode_checkout' );
function brown_remove_billing_postcode_checkout( $fields ) {
$error = '';
if (isset($_POST['post_data'])) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
$billing_delivery_date = $post_data['billing_delivery_date'];
if ( $billing_delivery_date = !NULL ) {
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Africa/Kampala'));
$order_day = $date->format('Y-m-d H:i:s');
$date = new DateTime(date('Y-m-d', strtotime(str_replace('/' , '-', $billing_delivery_date))));
//If the date picked is greater than the current date
if (strtotime($order_day) > strtotime($date) {
//Make an array of values from 11am to 4pm to pass to $fields['billing']['billing_serving'] options array
} elseif ($order_day == $date) {
//If the Make an array of values from 11am to 4pm to pass to $fields['billing']['billing_serving'] options array
//Get the current time + one hour. Append hour intervals from next top of the hour until 4pm.
}
}
// Add New Fields
$fields['billing']['billing_serving'] = array(
'label' => __('Delivery Time', 'woocommerce'),
'placeholder' => _x('Time for your delivery', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-last'),
'clear' => false,
'autocomplete' => false,
'type' => 'select',
'options' => array(
//Generated array goes here.
'11am' => __('11:00am', 'woocommerce' ),
'1130am' => __('11:30am', 'woocommerce' ),
'12noon' => __('12:00pm', 'woocommerce' ),
'1230pm' => __('12:30pm', 'woocommerce' ),
'1pm' => __('1:00pm', 'woocommerce' ),
'130pm' => __('1:30pm', 'woocommerce' ),
'2pm' => __('2:00pm', 'woocommerce' ),
'230pm' => __('2:30pm', 'woocommerce' ),
'3pm' => __('3:00pm', 'woocommerce' ),
'330pm' => __('3:30pm', 'woocommerce' ),
'4pm' => __('4:00pm', 'woocommerce' )
)
);
return $fields;
}