const radios = document.querySelectorAll('#qty-options input') const originalPrice = document.querySelector('#original-price') const currentPrice = document.querySelector('#current-price') // Initiate value for (let option of radios) { if (option.checked) { updateQueryString('variant', option.value) updatePrice() } } // Add event listeners to each radio button for (let option of radios) { option.addEventListener('change', (event) => { updateQueryString('variant',event.target.value) updatePrice() }); } function updatePrice() { const variants = JSON.parse('[{"id":40079386869911,"title":"1 Pack","option1":"1 Pack","option2":null,"option3":null,"sku":"Wild-1","requires_shipping":true,"taxable":false,"featured_image":null,"available":true,"name":"Wildflower Honey - 1 Pack","public_title":"1 Pack","options":["1 Pack"],"price":999,"weight":450,"compare_at_price":null,"inventory_quantity":17,"inventory_management":"shopify","inventory_policy":"continue","barcode":"627843344902","requires_selling_plan":false,"selling_plan_allocations":[]},{"id":40079386902679,"title":"2 Pack","option1":"2 Pack","option2":null,"option3":null,"sku":"Wild-2","requires_shipping":true,"taxable":false,"featured_image":null,"available":true,"name":"Wildflower Honey - 2 Pack","public_title":"2 Pack","options":["2 Pack"],"price":1899,"weight":450,"compare_at_price":1998,"inventory_quantity":-1,"inventory_management":"shopify","inventory_policy":"continue","barcode":"","requires_selling_plan":false,"selling_plan_allocations":[]},{"id":40079386935447,"title":"4 Pack","option1":"4 Pack","option2":null,"option3":null,"sku":"Wild-3","requires_shipping":true,"taxable":false,"featured_image":null,"available":true,"name":"Wildflower Honey - 4 Pack","public_title":"4 Pack","options":["4 Pack"],"price":3699,"weight":450,"compare_at_price":3996,"inventory_quantity":0,"inventory_management":"shopify","inventory_policy":"continue","barcode":"","requires_selling_plan":false,"selling_plan_allocations":[]}]') const urlParams = new URLSearchParams(window.location.search); const variantId = urlParams.get('variant') const item = variants.find(v => v.id.toString() === variantId) if (item.compare_at_price) { originalPrice.innerHTML = `$${(item.compare_at_price / 100).toFixed(2)}` originalPrice.classList.add = `mr-2` } else { originalPrice.innerHTML = `` originalPrice.classList.remove = `mr-2` } currentPrice.innerHTML = `$${(item.price / 100).toFixed(2)}` } function updateQueryString(key, value, url) { if (!url) url = window.location.href; let updated = '' var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"), hash; if (re.test(url)) { if (typeof value !== 'undefined' && value !== null) { updated = url.replace(re, '$1' + key + "=" + value + '$2$3'); } else { hash = url.split('#'); url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, ''); if (typeof hash[1] !== 'undefined' && hash[1] !== null) { url += '#' + hash[1]; } updated = url; } } else { if (typeof value !== 'undefined' && value !== null) { var separator = url.indexOf('?') !== -1 ? '&' : '?'; hash = url.split('#'); url = hash[0] + separator + key + '=' + value; if (typeof hash[1] !== 'undefined' && hash[1] !== null) { url += '#' + hash[1]; } updated = url; } else { updated = url; } } window.history.replaceState({ path: updated }, '', updated); }