Last active
April 28, 2018 00:18
-
-
Save gterrill/24596dd5b30468897ad4a40c74807cc6 to your computer and use it in GitHub Desktop.
Restrict options when variants represent quantity
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
// When a date/time is chosen, update the variant option that represents the number | |
// of people attending the event. | |
$('form[action="/cart/add"]').on('bta.datetimeChange', function(event, form) { | |
var index = form.getAvailability() - 1, | |
quantitySelector = $('select[data-option="option1"]', $(this)); // assumes the variant option quantity selector is option1 | |
// clear any existing disabled options | |
quantitySelector.find('option').removeAttr('disabled'); | |
// disabled options > available | |
quantitySelector.find(' option:gt(' + index + ')').attr('disabled', 'disabled'); | |
// select first available | |
if (quantitySelector.find('option:first').not(':disabled').length > 0 && quantitySelector.find('option:selected').is(':disabled')) { | |
quantitySelector.find('option:first').not(':disabled').prop('selected', true) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment