I have a piece of code which is used on a Shopify website. Basically there are 3 delivery options, one of which is Saturday delivery which disables all days apart from Saturdays if that radio is selected.
<div class="delivery-radio">
<h3>1. Choose your delivery type:</h3>
<input type="radio" name="attributes[delivery-type]" value="Standard" checked> Standard - Delivery by 6pm (£4.95)<br>
<input type="radio" name="attributes[delivery-type]" value="Pre 12" id="pre_12"> Express - Delivery by 12pm (£6.25)<br>
<input type="radio" name="attributes[delivery-type]" value="saturday" id="saturday"> Saturday - Delivery by 6pm (£6.95)
</div>
<div style="width:300px; clear:both;">
<h3>2. Pick a delivery date:</h3>
<p>
<input id="date" type="text" name="attributesFebruary 22, 2018" value="{{ cart.attributes.date }}" required readonly="readonly" /> <span id="calendar_click" class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
</p>
</div>
What I need is a function that doesn’t break my current code that disables UK Bank Holidays. I have seen this can be done manually by adding the dates and disabling them but everything I have tried doesn’t work or breaks my current code.
Here is a snippet of my code, if any jquery/javascript guru’s can help out that would be appreciated as I am not too advanced in this area.
jQuery(function() {
$('input[name="attributes[delivery-type]"]').on('click', function(){
$("#date").datepicker("destroy");
if($(this).val() == "saturday") {
$("#date").datepicker({
dateFormat: 'dd-mm-yy',
minDate: +1,
maxDate: '+2M',
beforeShowDay: function(date){ return February 22, 2018}
});
} else {
$("#date").datepicker(
{
dateFormat: 'dd-mm-yy',
minDate: +1,
maxDate: '+2M',
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 0 && day != 1), ''];
}
});
}
});
$("#date").datepicker(
{
dateFormat: 'dd-mm-yy',
minDate: +1,
maxDate: '+2M',
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 0 && day != 1), ''];
}
});
});
2
Answers
By default I have disabled saturday and sunday deliveries by keeping daysOfWeekDisabled: [0,6] and for Saturday delivery option, I have kept it like – daysOfWeekDisabled: [0,1,2,3,4,5]. Bank holidays list resides in datesDisabled. Also mindate and maxDate are startDate and endDate.
datesDisabled
appears to be functionality for the bootstrap datepicker.You can add a check to the
beforeShowDay
function, I would separate this from the different parts and use variables as necessary.translate the Saturday deliveries from
to
beforeShowDay: saturdayOnly
and your default
to
beforeShowDay: notSunMon
then add the handling for these two options:
“`
“`
This uses a function to format your dates, to make sure they’re all correct, and adds css classes. See the jsfiddle for how these are implemented.
Just a note,
days !=0 && day != 1
checks for “not a Sunday/Monday”Try it out with jsfiddle: https://jsfiddle.net/vreemt/sbrtyvoz/