Last active
December 16, 2015 04:09
-
-
Save gterrill/5375390 to your computer and use it in GitHub Desktop.
Capturing a start date and number of days
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
{% comment %} | |
BookThatApp auto installed snippet. Changes made to this file will be lost if installed again. | |
{% endcomment %} | |
{% if product.metafields.bookthatapp.product_id %} | |
<div class="selector-wrapper"> | |
{% capture attribute %}booking-start{% endcapture %} | |
<label for="{{ attribute }}">Rental Start Date:</label> | |
<input id="{{ attribute }}" type="text" name="properties[{{ attribute }}]" class="datepicker bta required bta-load-enable styled-input" autocomplete="off" size="12" data-handle="{{ product.handle }}" data-variant="{{ product.variants.first.id}}" data-mindate="{{ product.metafields.bookthatapp.lead_time }}" disabled="disabled" /> | |
<label id="booked-out"></label> | |
</div> | |
{% capture attribute %}booking-finish{% endcapture %} | |
<input id="{{ attribute }}" type="hidden" name="properties[{{ attribute }}]" /> | |
<script type="text/javascript"> | |
function isAvailable(date) { | |
var result = true; | |
bta.checkAvailability(date, $('#booking-start'), { | |
blackedout: function() { | |
result = false; | |
}, | |
unscheduled: function() { | |
result = false; | |
}, | |
availability: function(capacity, bookingCount) { | |
if (capacity == 0) { | |
result = false; | |
} else if (capacity > 0 && bookingCount >= capacity) { // product is booked out | |
result = false; | |
} | |
} | |
}) | |
return result; | |
} | |
function checkAvailability(start, qty) { | |
var available = true; | |
for (var i = 0; i < qty && available; i++) { | |
var proposed = new Date(); | |
proposed.setDate(start.getDate() + i); | |
available = isAvailable(proposed); | |
} | |
return available; | |
} | |
function updateFinishDate() { | |
var qty = parseInt($('#quantity').val(), 10), | |
start = $('#booking-start').datepicker('getDate'), | |
finish = start, | |
available = checkAvailability(start, qty); | |
if (!available) { | |
$('#booked-out').text('The product is not available at the requested time.'); | |
$('#add').attr('disabled', 'disabled'); | |
} else { | |
$('#booked-out').text(''); | |
$('#add').removeAttr('disabled'); | |
} | |
finish.setDate(start.getDate() + qty); | |
var parts = bta.YMDDateString(finish).substring(0, 10).split('-'); | |
$('#booking-finish').val(parts[1] + "/" + parts[2] + "/" + parts[0]); | |
} | |
$(document).ready(function() { | |
$('#quantity').change(function() { | |
updateFinishDate(); | |
}); | |
}) | |
var bta = { | |
product_id: {{ product.id }}, | |
callbacks: { | |
dateSelected: function(inst, date) { | |
updateFinishDate(); | |
} | |
} | |
} | |
</script> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment