(function () { const cartGiftHtml = `
You are {{amount}} away from a Free Gift!
Congrats!You get a free gift with your order
Get your free beanie
Free with orders over {{amount}}
Free gift {{amount}}
`; function docReady(fn) { // see if DOM is already available if (document.readyState === 'complete' || document.readyState === 'interactive') { // call on next available tick setTimeout(fn, 1); } else { document.addEventListener('DOMContentLoaded', fn); } } docReady(function () { if (window.location.origin === 'https://greencoffeeco.com') { //show free gift for the first time const freeGiftSection = document.getElementById("custom-upcart-script"); if (!freeGiftSection) { document.getElementsByClassName("product-form__submit")[0].addEventListener("click", () => { setTimeout(() => { let upcartCustomScript = document.createElement('script'); upcartCustomScript.setAttribute("id", "custom-upcart-script"); upcartCustomScript.setAttribute('src', 'https://upcart.herokuapp.com/script-tag.js/gid://shopify/Shop/70024626462?shop=greencoffeeco.myshopify.com'); document.head.appendChild(upcartCustomScript); }, 1000); }); } //reflect the free gift after changing the quantity of the cart drawer const input = document.getElementById("Drawer-quantity-1"); if (input) { input.addEventListener("change", (event) => { setTimeout(() => { let upcartCustomScript = document.createElement('script'); upcartCustomScript.setAttribute('src', 'https://upcart.herokuapp.com/script-tag.js/gid://shopify/Shop/70024626462?shop=greencoffeeco.myshopify.com'); document.head.appendChild(upcartCustomScript); }, 1000); }); } } if (window.location.origin === 'https://diamondpaintingforyou.com') { // update the cart after decreasing the number of the products in the cart document.getElementsByClassName("quantity--input__incr")[3].addEventListener("click", () => { // @ts-ignore const formDivs = document.querySelectorAll(`form[action="${window.Shopify.routes.root}cart"],form[action^="${window.Shopify.routes.root}cart?"]`); if (formDivs.length !== 0) { formDivs.forEach(formDiv => { // @ts-ignore formDiv.insertAdjacentHTML('beforeBegin', '
'); }); } setTimeout(() => { let upcartCustomScript = document.createElement('script'); upcartCustomScript.setAttribute("id", "custom-upcart-script"); upcartCustomScript.setAttribute('src', 'https://upcart.herokuapp.com/script-tag.js/gid://shopify/Shop/53632696514?shop=daimond-painting-for-you.myshopify.com'); document.head.appendChild(upcartCustomScript); }, 1000); }); // update the cart after increasing the number of the products in the cart document.getElementsByClassName("quantity--input__decr")[3].addEventListener("click", () => { // @ts-ignore const formDivs = document.querySelectorAll(`form[action="${window.Shopify.routes.root}cart"],form[action^="${window.Shopify.routes.root}cart?"]`); if (formDivs.length !== 0) { formDivs.forEach(formDiv => { // @ts-ignore formDiv.insertAdjacentHTML('beforeBegin', '
'); }); } setTimeout(() => { let upcartCustomScript = document.createElement('script'); upcartCustomScript.setAttribute("id", "custom-upcart-script"); upcartCustomScript.setAttribute('src', 'https://upcart.herokuapp.com/script-tag.js/gid://shopify/Shop/53632696514?shop=daimond-painting-for-you.myshopify.com'); document.head.appendChild(upcartCustomScript); }, 1000); }); } function getCookie(name) { const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) return parts.pop().split(';').shift(); } const installType = "cart"; const installLocation = "above"; // @ts-ignore if (installType !== 'custom') { const customFormDivs = document.querySelectorAll('.free-gift-wrapper-installation'); if (customFormDivs.length !== 0) { customFormDivs.forEach(formDiv => { // @ts-ignore formDiv.style.display = 'none'; }); } let customDiscountWrapperDivs = document.querySelectorAll('#free-gift-wrapper,.free-gift-wrapper'); customDiscountWrapperDivs.forEach(div => div.classList.add('disabled')); const impulseCartDrawerTheme = document.getElementById('CartDrawerForm'); const prestigeCartDrawerTheme = document.getElementsByClassName('Cart Drawer__Content'); // Impulse theme if (impulseCartDrawerTheme) { // @ts-ignore impulseCartDrawerTheme.insertAdjacentHTML(installLocation === 'above' ? 'afterbegin' : 'afterend', '
'); //Prestige theme } else if (prestigeCartDrawerTheme.length) { // @ts-ignore prestigeCartDrawerTheme[0].insertAdjacentHTML(installLocation === 'above' ? 'afterbegin' : 'afterend', '
'); //Another themes } else { // @ts-ignore const formDivs = document.querySelectorAll(`form[action="${window.Shopify.routes.root}cart"],form[action^="${window.Shopify.routes.root}cart?"]`); if (formDivs.length !== 0) { if (window.location.origin === 'https://hazelcandleco.com') { // @ts-ignore formDivs[1].insertAdjacentHTML(installLocation === 'above' ? 'beforebegin' : 'afterend', '
'); } else { formDivs.forEach(formDiv => { // @ts-ignore formDiv.insertAdjacentHTML(installLocation === 'above' ? 'beforebegin' : 'afterend', '
'); }); } } } } // @ts-ignore if (installType === 'custom') { const formDivs = document.querySelectorAll('.free-gift-wrapper'); if (formDivs.length !== 0) { formDivs.forEach(formDiv => { // @ts-ignore formDiv.style.display = 'none'; }); } } // const currencySymbolFromCookies = getCookie('cart_currency'); const currencySymbol = "€"; const startVariantId = 41615570305224; let variantId = localStorage.getItem("variantId") === null ? 'PRODUCT_ID' : localStorage.getItem("variantId"); localStorage.setItem("startVariantId", startVariantId); const campaignId = "B2OUZ46hkxQJRDxcNPWm"; const cartTreshold = 19950; let productAvailable = false; let giftAdded = false; const selectedVariantIds = "41615570305224,41615570337992,41615570370760"; let selectedVariantIdsArr = []; // @ts-ignore if (selectedVariantIds === 'none') { selectedVariantIdsArr = []; } else { selectedVariantIdsArr = selectedVariantIds.split(','); } let customDiscountWrapperDivs = document.querySelectorAll('#free-gift-wrapper:not(.disabled),.free-gift-wrapper:not(.disabled), .free-gift-wrapper-installation:not(.disabled)'); for (let i = 0; i < customDiscountWrapperDivs.length; i++) { let wrapper = customDiscountWrapperDivs[i]; wrapper.innerHTML = cartGiftHtml; let freeGiftDiv = wrapper.querySelector('.free-gift'); let freeGiftAwayDiv = wrapper.querySelector('.free-gift__away'); let freeGiftAwayDivContent = freeGiftAwayDiv.innerHTML; let freeGiftCongratsDiv = wrapper.querySelector('.free-gift__congrats'); let freeGiftProgressBarDiv = wrapper.querySelector('.free-gift__progress-bar'); let freeGiftCardImageDiv = wrapper.querySelector('.free-gift__card-image'); let freeGiftCardTitleDiv = wrapper.querySelector('.free-gift__card-title'); let freeGiftCardDescriptionDiv = wrapper.querySelector('.free-gift__card-description'); let freeGiftCardDescriptionDivContent = freeGiftCardDescriptionDiv.innerHTML; let freeGiftCardButton = wrapper.querySelector('.free-gift__card-button'); let freeGiftCardAddedDiv = wrapper.querySelector('.free-gift__card-gift-added'); let freeGiftCardAddedDivContent = freeGiftCardAddedDiv.innerHTML; const select = document.createElement('select'); function addGift() { // @ts-ignore fetch(`${window.Shopify.routes.root}cart/add.js`, { body: JSON.stringify({ id: parseInt(localStorage.getItem("variantId") === null ? variantId : localStorage.getItem("variantId")), quantity: 1, }), credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'xmlhttprequest' /* XMLHttpRequest is ok too, it's case insensitive */, }, method: 'POST', }) .then(function (response) { return response.json(); }) .then(function (json) { // @ts-ignore location.assign(`${window.Shopify.routes.root}cart`); }); } function updateCartAttributes(hasGift) { // @ts-ignore fetch(`${window.Shopify.routes.root}cart/update.js`, { body: JSON.stringify({ attributes: hasGift ? { FreeGiftCampaignId: campaignId } : { FreeGiftCampaignId: null }, }), credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'xmlhttprequest' /* XMLHttpRequest is ok too, it's case insensitive */, }, method: 'POST', }); } function computeGiftAdded(cart) { let cartGiftProductItem = cart.items.find(x => x.id === parseInt(localStorage.getItem("variantId") === null ? variantId : localStorage.getItem("variantId"))); //console.log("cart attributes " + JSON.stringify(cart, null, 2)) if (cartGiftProductItem) { giftAdded = true; if (!Object.keys(cart.attributes).includes('FreeGiftCampaignId') || cart.attributes.FreeGiftCampaignId !== campaignId) { updateCartAttributes(true); } } else { giftAdded = false; if (Object.keys(cart.attributes).includes('FreeGiftCampaignId')) updateCartAttributes(false); localStorage.removeItem("variantId"); let startVariantId = localStorage.getItem("startVariantId"); localStorage.setItem("variantId", startVariantId); //localStorage.clear(); } } function computeCartSubtotal(cart) { const reducer = (acc, item) => acc + item.final_line_price; return cart.items.filter(x => x.id != variantId).reduce(reducer, 0); } function updateInterface(subtotal) { // @ts-ignore freeGiftProgressBarDiv.style.width = `${Math.min((subtotal / cartTreshold) * 100, 100)}%`; if (subtotal >= cartTreshold) { // @ts-ignore freeGiftAwayDiv.style.display = 'none'; // @ts-ignore freeGiftCongratsDiv.style.display = 'block'; // @ts-ignore freeGiftCardButton.disabled = false; if (giftAdded) { // @ts-ignore freeGiftCardButton.style.display = 'none'; select.style.display = 'none'; // @ts-ignore freeGiftCardAddedDiv.style.display = 'inline-flex'; } else { // @ts-ignore freeGiftCardButton.style.display = 'block'; // @ts-ignore freeGiftCardAddedDiv.style.display = 'none'; select.style.display = 'inline-block'; } } else { // @ts-ignore freeGiftAwayDiv.innerHTML = freeGiftAwayDivContent.replace('{{amount}}', `${currencySymbol}${(cartTreshold - subtotal) / 100}`); // @ts-ignore freeGiftAwayDiv.style.display = 'block'; // @ts-ignore freeGiftCongratsDiv.style.display = 'none'; // @ts-ignore freeGiftCardButton.disabled = true; // @ts-ignore freeGiftCardButton.style.display = 'block'; // @ts-ignore freeGiftCardAddedDiv.style.display = 'none'; } } let freeProductGiftHandle; fetch('/products/sunglasses.js') .then(response => { if (response.status !== 200) { console.log('Failed to load product', response); } return response.json(); }) .then(data => { productAvailable = data.available; //console.log("needed data " + JSON.stringify(data, null, 2)) // create variant selector select.style.textAlign = 'center'; let productVariants = []; if (selectedVariantIdsArr.length === 0) { productVariants = data.variants.filter(variant => variant.available === true); } else { productVariants = data.variants.filter(variant => variant.available === true && selectedVariantIdsArr.includes(variant.id.toString())); } select.name = 'productVariants'; select.id = 'campaignProductVariants'; select.style.border = '1px solid'; select.style.textAlign = 'center'; select.style.paddingLeft = '4px'; select.style.paddingRight = '14px'; select.style.paddingTop = '4px'; select.style.paddingBottom = '4px'; select.style.minWidth = '90px'; for (let i = 0; i < productVariants.length; i++) { const option = document.createElement('option'); option.value = productVariants[i].title; option.text = productVariants[i].title; select.appendChild(option); } const label = document.createElement("label"); label.innerHTML = "Choose your variants: "; label.htmlFor = "variants"; const ctaButton = document.getElementById('free-gift-cart-cta-button'); if (data.variants.length > 1) { ctaButton.parentNode.insertBefore(select, ctaButton); } //giftAdded === false && ctaButton.parentNode.insertBefore(label, select); (function createSelect() { select.addEventListener('change', function () { const variantName = select.options[select.selectedIndex].value; const selectedVariant = productVariants.filter(variant => variant.title === variantName); const selectedVariantId = selectedVariant[0].id; variantId = selectedVariantId; localStorage.setItem("variantId", variantId); }); })(); let variantId = localStorage.getItem("variantId"); if (variantId !== null) { let giftVariant = data.variants.filter(variant => variant.id === parseInt(variantId)); let giftPrice = giftVariant.length > 0 ? giftVariant[0].price : data.price; freeGiftCardAddedDiv.innerHTML = freeGiftCardAddedDivContent.replace('{{amount}}', `${currencySymbol} ${giftPrice / 100}`); // @ts-ignore // @ts-ignore freeGiftCardDescriptionDiv.innerHTML = freeGiftCardDescriptionDivContent.replace('{{amount}}', // @ts-ignore `${currencySymbol}${cartTreshold / 100}`); } else { freeGiftCardAddedDiv.innerHTML = freeGiftCardAddedDivContent.replace('{{amount}}', `${currencySymbol} ${data.price / 100}`); // @ts-ignore freeGiftCardDescriptionDiv.innerHTML = freeGiftCardDescriptionDivContent.replace('{{amount}}', // @ts-ignore `${currencySymbol}${cartTreshold / 100}`); } if (data.images && data.images[0]) { let productHandle = data.handle; freeProductGiftHandle = productHandle; let link = document.createElement('a'); link.href = '/products/' + productHandle; link.target = '_blank'; let img = document.createElement('img'); img.src = `http:${data.images[0]}`; freeGiftCardImageDiv.appendChild(link); freeGiftCardImageDiv.querySelector('a').appendChild(img); } // @ts-ignore fetch(window.Shopify.routes.root + 'cart.js') .then(response => response.json()) .then(data => { freeGiftCardButton.addEventListener('click', () => { addGift(); }); computeGiftAdded(data); updateInterface(computeCartSubtotal(data)); if (productAvailable) { // @ts-ignore freeGiftDiv.style.display = 'flex'; } }); }); const open = window.XMLHttpRequest.prototype.open; function openReplacement() { this.addEventListener("load", function () { // @ts-ignore if (window.location.origin !== "https://inkopious.com" && [ // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/add.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/update.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/change.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/clear.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart` // @ts-ignore ].includes(`${window.location.origin}${window.Shopify.routes.root}cart`)) { // @ts-ignore fetch(window.Shopify.routes.root + 'cart.js') .then(response => response.json()) .then(data => { computeGiftAdded(data); updateInterface(computeCartSubtotal(data)); }); } }); return open.apply(this, arguments); } window.XMLHttpRequest.prototype.open = openReplacement; (function (ns, fetch) { if (typeof fetch !== 'function') return; ns.fetch = function () { const response = fetch.apply(this, arguments); response.then(res => { if ([ // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/add`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/update`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/change`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/clear`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/add.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/update.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/change.js`, // @ts-ignore `${window.location.origin}${window.Shopify.routes.root}cart/clear.js` ].includes(res.url)) { res .clone() .json() .then(data => { if (arguments[1].body !== JSON.stringify({ attributes: { FreeGiftCampaignId: campaignId }, }) && arguments[1].body !== JSON.stringify({ attributes: { FreeGiftCampaignId: null }, })) { // @ts-ignore fetch(window.Shopify.routes.root + 'cart.js') .then(response => response.json()) .then(data => { computeGiftAdded(data); updateInterface(computeCartSubtotal(data)); }); } }); } }); return response; }; })(window, window.fetch); } //custom code for the specific client shop if (window.location.origin === 'https://hazelcandleco.com') { const freeGiftBlocks = document.getElementsByClassName("free-gift-wrapper"); if (window.location.href !== 'https://hazelcandleco.com/cart' && freeGiftBlocks.length) { // @ts-ignore freeGiftBlocks[0].style.display = "none"; } if (window.location.href === 'https://hazelcandleco.com/cart' && freeGiftBlocks.length === 2) { // @ts-ignore freeGiftBlocks[0].style.display = "none"; } } }); })()