I've added DatePicker to my checkout page for customers to pick the delivery date. Also added a script to disable past dates (obviously) and delivery on some holidays.
Here is the script:
<script>
var disableddates = ["14-02-2018", "25-12-2018"];
function DisableSpecificDates(date) {
var string = jQuery.datepicker.formatDate("dd-mm-yy", date);
return [disableddates.indexOf(string) == -1];
}
jQuery(function() {
jQuery("#billing_delivery_date").datepicker({
dateFormat: "DD, d MM, yy",
beforeShowDay: DisableSpecificDates,
minDate: new Date()
});
});
</script>
And it works perfectly until I try to put some restrictions on it. I need just disable holiday delivery for some categories, not for all. I did it like that:
function add_checkout_script() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// if a product is not in our cat, bail out since we know the cat is not alone
if ( has_term( my_category, 'product_cat', $cart_item['data']->id ) ) {
wp_enqueue_script( 'newscript', get_stylesheet_directory_uri() . '/restrict_day_script.js', array( 'jquery' ));
}
}
}
add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );
Also tried just paste the script:
function add_checkout_script() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// if a product is not in our cat, bail out since we know the cat is not alone
if ( has_term( my_category, 'product_cat', $cart_item['data']->id ) ) {
?>
<script>
var disableddates = ["14-02-2018", "25-12-2018"];
function DisableSpecificDates(date) {
var string = jQuery.datepicker.formatDate("dd-mm-yy", date);
return [disableddates.indexOf(string) == -1];
}
jQuery(function() {
jQuery("#billing_delivery_date").datepicker({
dateFormat: "DD, d MM, yy",
beforeShowDay: DisableSpecificDates,
minDate: new Date()
});
});
</script>
<?php
}
}
}
add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );
What am I doing wrong?