Chooses al of the options from a product URL containing the variant_id parameter in Jumpseller

Alejandro Troncoso
2021-03-17T20:05:56Z

open_variant_from_url_parameter.js

| $(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
| })
| }
| });