Skip to content

Instantly share code, notes, and snippets.

@gterrill
Last active April 28, 2018 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gterrill/24596dd5b30468897ad4a40c74807cc6 to your computer and use it in GitHub Desktop.
Save gterrill/24596dd5b30468897ad4a40c74807cc6 to your computer and use it in GitHub Desktop.
Restrict options when variants represent quantity
// 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