Last active
December 17, 2015 11:29
-
-
Save gterrill/5602763 to your computer and use it in GitHub Desktop.
Setting quantity max based on selected variant and date range
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
<script type="text/javascript"> | |
var bta = { | |
product_id: {{ product.id }}, | |
callbacks: { | |
dateSelected: function(input, date) { | |
checkAvailable(); | |
}, | |
available: function(data) { // invoked after availability data from BookThatApp is loaded via Ajax. | |
checkAvailable(); | |
} | |
} | |
}; | |
function checkAvailable() { | |
var start = $('#booking-start').datepicker('getDate'), | |
finish = $('#booking-finish').datepicker('getDate'), // assign this to start if you don't have an end date | |
capacity = bta.availableCapacity($('#product-select option:selected').val(), start, finish), | |
qty = parseInt($('#quantity').val(), 10); | |
$('#quantity').attr('max', capacity); | |
if (capacity == 0) { | |
$('#add-to-cart').attr('disabled', 'disabled'); | |
} else { | |
$('#add-to-cart').removeAttr('disabled'); | |
} | |
if (qty > capacity) { | |
$('#quantity').val(capacity); | |
} else if (qty == 0 && capacity > 0) { | |
$('#quantity').val(1); | |
} | |
return capacity > 0; | |
} | |
// add this to theme.liquid if jquery loaded after page load | |
$('#quantity').change(function() { | |
checkAvailable(); // defined in booking-form snippet | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do I just add this to the booking-form.liquid snippet in place of:
var bta = {
productId: {{ product.id }}
}
?
I am using the 'New Standard' theme if that matters. Regards