Skip to content

Instantly share code, notes, and snippets.

@gterrill
Last active August 29, 2015 13:57
Show Gist options
  • Save gterrill/9516740 to your computer and use it in GitHub Desktop.
Save gterrill/9516740 to your computer and use it in GitHub Desktop.
Select variant according to number of days
var selectVariant = function(form) {
var start = form.getStartDate(),
finish = form.getFinishDate();
if (start && finish) {
var quantity = bta.dayDelta(start, finish), minutes = (quantity * 1440);
$('#quantity').val(quantity);
var index = -1, low = 0,
configs = bta.parseVariantConfigs(form.datepicker('start').attr('data-bta-variant-config'));
for (var id in configs) {
var config = configs[id],
duration = form.getDuration();
if (minutes > low) {
index += 1;
}
low = duration;
}
// do stuff here to update the currently selected variant
// this is for the 'startup' theme which uses Backbone
$('#product-select option').removeAttr('selected');
$('#product-select option:eq(' + index + ')').attr('selected', 'selected');
$('#product-select-option-0 option').removeAttr('selected');
$('#product-select-option-0 option:eq(' + index + ')').attr('selected', 'selected');
$('#product-select-option-0').trigger('change');
}
};
$('form[action="/cart/add"]').on('bta.datetimeChange', function(event, form) {
selectVariant(form);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment