Last active
July 18, 2016 05:46
-
-
Save gterrill/5381626 to your computer and use it in GitHub Desktop.
Matches the date range selected to a variant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This code is now deprecated. Please contact the helpdesk if you need help with choosing a variant based on the date range chosen | |
*/ | |
jQuery.fn.reverse = [].reverse; | |
function selectVariant() { | |
var start = $('#booking-start').datepicker('getDate'), | |
finish = $('#booking-finish').datepicker('getDate'); | |
if (start && finish) { | |
var oneDayMinutes = 24*60, // hours*minutes | |
oneDayMillis = oneDayMinutes * 60 * 1000, // seconds*milliseconds | |
diffDays = Math.round(Math.abs((start.getTime() - finish.getTime())/oneDayMillis)) + 1, | |
diffMinutes = diffDays * oneDayMinutes, | |
value = $('select[name=id] option:last-child').attr('data-val'); // assume 6+ days | |
// look up variants and find first duration large enough | |
$('select[name=id] option').reverse().each(function() { | |
var durationMinutes = parseInt($(this).attr('data-duration'), 10); | |
if (durationMinutes <= diffMinutes) { | |
value = $(this).attr('data-val'); | |
return false; // found our variant | |
} | |
}) | |
$('#product-select-option-0').val(value).trigger('change'); | |
} | |
} | |
$('.datepicker').datepicker({ | |
minDate: 1, | |
dateFormat: 'dd-mm-yy', | |
beforeShow: function(input) { | |
jQuery(input).addClass('openDatepicker'); | |
}, | |
onClose: function (dateText, inst) { | |
jQuery(inst.input).removeClass('openDatepicker'); | |
}, | |
onSelect: function(dateText, inst) { | |
selectVariant(); | |
} | |
}); | |
$('#ui-datepicker-div').css('clip', 'auto'); // fix for not showing properly - see http://stackoverflow.com/questions/2682259/jquery-ui-datepicker-not-displaying | |
$('form[action="/cart/add"]').submit(function() { | |
var start = $('#booking-start').datepicker('getDate'); | |
$('#booking-start').val(bta.ISODateString(start)); | |
var finish = $('#booking-finish').datepicker('getDate'); | |
$('#booking-finish').val(bta.ISODateString(finish)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment