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":29534188237,"title":"S","option1":"S","option2":null,"option3":null,"sku":"3512839","requires_shipping":true,"taxable":true,"featured_image":{"id":19955894669,"product_id":8624729229,"position":1,"created_at":"2016-12-17T14:51:55-05:00","updated_at":"2016-12-17T14:51:55-05:00","alt":null,"width":1000,"height":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315","variant_ids":[29534188237,29534188301,29534188365,29534188429,29534188493]},"available":true,"name":"Nude Calligraphic Ladies' short sleeve t-shirt - S","public_title":"S","options":["S"],"price":2567,"weight":99,"compare_at_price":null,"inventory_quantity":1,"inventory_management":null,"inventory_policy":"deny","barcode":null,"featured_media":{"alt":null,"id":128075497565,"position":1,"preview_image":{"aspect_ratio":1.0,"height":1000,"width":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315"}},"requires_selling_plan":false,"selling_plan_allocations":[]},{"id":29534188301,"title":"M","option1":"M","option2":null,"option3":null,"sku":"2345844","requires_shipping":true,"taxable":true,"featured_image":{"id":19955894669,"product_id":8624729229,"position":1,"created_at":"2016-12-17T14:51:55-05:00","updated_at":"2016-12-17T14:51:55-05:00","alt":null,"width":1000,"height":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315","variant_ids":[29534188237,29534188301,29534188365,29534188429,29534188493]},"available":true,"name":"Nude Calligraphic Ladies' short sleeve t-shirt - M","public_title":"M","options":["M"],"price":2567,"weight":99,"compare_at_price":null,"inventory_quantity":0,"inventory_management":null,"inventory_policy":"deny","barcode":null,"featured_media":{"alt":null,"id":128075497565,"position":1,"preview_image":{"aspect_ratio":1.0,"height":1000,"width":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315"}},"requires_selling_plan":false,"selling_plan_allocations":[]},{"id":29534188365,"title":"L","option1":"L","option2":null,"option3":null,"sku":"4952093","requires_shipping":true,"taxable":true,"featured_image":{"id":19955894669,"product_id":8624729229,"position":1,"created_at":"2016-12-17T14:51:55-05:00","updated_at":"2016-12-17T14:51:55-05:00","alt":null,"width":1000,"height":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315","variant_ids":[29534188237,29534188301,29534188365,29534188429,29534188493]},"available":true,"name":"Nude Calligraphic Ladies' short sleeve t-shirt - L","public_title":"L","options":["L"],"price":2567,"weight":105,"compare_at_price":null,"inventory_quantity":1,"inventory_management":null,"inventory_policy":"deny","barcode":null,"featured_media":{"alt":null,"id":128075497565,"position":1,"preview_image":{"aspect_ratio":1.0,"height":1000,"width":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315"}},"requires_selling_plan":false,"selling_plan_allocations":[]},{"id":29534188429,"title":"XL","option1":"XL","option2":null,"option3":null,"sku":"6737629","requires_shipping":true,"taxable":true,"featured_image":{"id":19955894669,"product_id":8624729229,"position":1,"created_at":"2016-12-17T14:51:55-05:00","updated_at":"2016-12-17T14:51:55-05:00","alt":null,"width":1000,"height":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315","variant_ids":[29534188237,29534188301,29534188365,29534188429,29534188493]},"available":true,"name":"Nude Calligraphic Ladies' short sleeve t-shirt - XL","public_title":"XL","options":["XL"],"price":2567,"weight":111,"compare_at_price":null,"inventory_quantity":1,"inventory_management":null,"inventory_policy":"deny","barcode":null,"featured_media":{"alt":null,"id":128075497565,"position":1,"preview_image":{"aspect_ratio":1.0,"height":1000,"width":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315"}},"requires_selling_plan":false,"selling_plan_allocations":[]},{"id":29534188493,"title":"2XL","option1":"2XL","option2":null,"option3":null,"sku":"9266446","requires_shipping":true,"taxable":true,"featured_image":{"id":19955894669,"product_id":8624729229,"position":1,"created_at":"2016-12-17T14:51:55-05:00","updated_at":"2016-12-17T14:51:55-05:00","alt":null,"width":1000,"height":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315","variant_ids":[29534188237,29534188301,29534188365,29534188429,29534188493]},"available":true,"name":"Nude Calligraphic Ladies' short sleeve t-shirt - 2XL","public_title":"2XL","options":["2XL"],"price":2767,"weight":119,"compare_at_price":null,"inventory_quantity":1,"inventory_management":null,"inventory_policy":"deny","barcode":null,"featured_media":{"alt":null,"id":128075497565,"position":1,"preview_image":{"aspect_ratio":1.0,"height":1000,"width":1000,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0723\/5823\/products\/mockup-53bae6d0.jpg?v=1482004315"}},"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); }