Skip to content

Instantly share code, notes, and snippets.

@aatronco
Created March 17, 2021 20:05
Show Gist options
  • Save aatronco/dc100bd1928364ffe4222a55f01f43b0 to your computer and use it in GitHub Desktop.
Save aatronco/dc100bd1928364ffe4222a55f01f43b0 to your computer and use it in GitHub Desktop.
Chooses al of the options from a product URL containing the variant_id parameter in Jumpseller
$(document).ready(function() {
// Get the variant_id from the parameter
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('variant_id')) {
const variant_id = urlParams.get('variant_id')
var update_function = function(data) {
// Find the variant
active_variant = data.variants.find(item => item.id == variant_id)
// Choose each option of the variant
jQuery.each(active_variant.options, function(i, item) {
$('select[name=' + item.name + '] option').filter(function() {
return $(this).html() == item.value;
}).prop("selected", true)
});
// Force a ntive change event
selects = document.getElementsByClassName("prod-options");
selects[0].dispatchEvent(new Event("change"));
};
// Get Current Product info and apply callback
Jumpseller.getCurrentProductInfo({
callback: update_function
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment