Slater Designs Pill Trucker Hat
Choose options


{ try { if (Object.keys(details).length > 0) { resolve(details); } else { fetch(`https://${window.location.hostname}/cart.js`, { headers: { 'Content-Type': 'application/json; charset=UTF-8', }, }) .then((response) => { if (response.status === 200) { return response.json(); } reject(undefined); }) .then((data) => resolve(data)) .catch(() => resolve(undefined)); } } catch (error) { console.log('Error while getting cart details: ', error); reject(undefined); } }); } function cartUpdate(items) { return new Promise((resolve, reject) => { try { fetch('/cart/change.js', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(items), }) .then((response) => { if (response.status === 200) { return response.json(); } return undefined; }) .then((data) => resolve(data)) .catch(() => reject(undefined)); } catch (error) { console.log('Error while updating cart: ', error); reject(error); } }); } function getAvailableServices(myShopifyDomain) { return new Promise((resolve, reject) => { if (localStorage.getItem('bookeasy-setup')) { let bookeasySetup = JSON.parse(localStorage.getItem('bookeasy-setup')); resolve(bookeasySetup?.availableServices); fetch(`${SCRIPT_HOST_NAME}/api/bookeasy/validate/localSetup`, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: JSON.stringify({ myShopifyDomain: myShopifyDomain, servicesUpdatedAt: bookeasySetup.servicesUpdatedAt, setupUpdatedAt: bookeasySetup.setupUpdatedAt, subscriptionUpdatedAt: bookeasySetup.subscriptionUpdatedAt }), }) .then((response) => { if (response.status === 200) { return response.json(); } }) .then((data) => { if (data?.clearLocalSetup) { localStorage.removeItem('bookeasy-setup'); } }) .catch(() => { localStorage.removeItem('bookeasy-setup'); }); } else { fetch(`${SCRIPT_HOST_NAME}/api/availableServices?myShopifyDomain=${myShopifyDomain}`, { method: 'GET', headers: { 'Content-Type': 'application/json; charset=UTF-8', }, }) .then((response) => response.json()) .then((response) => { if (!response.clearLocalSetup) { localStorage.setItem('bookeasy-setup', JSON.stringify(response)); } resolve(response?.availableServices); }) .catch(() => reject(undefined)); } }); } function processCartItems(details = {}, services = [], productWithServiceFound = false) { if (Shopify.shop) { let myShopifyDomain = Shopify.shop; getCartDetails(details).then((cartDetails) => { if (cartDetails?.items) { let items = cartDetails.items; //Services is passed as param so that local setup call is initiated only once not for every cart update if (services.length > 0) { findMatchProductWithService(items, services, productWithServiceFound); } else { getAvailableServices(myShopifyDomain).then((availableServices) => { findMatchProductWithService(items, availableServices, productWithServiceFound); }); } } }); } } function findMatchProductWithService(items = [], availableServices = [], productWithServiceFound) { let activeServices = availableServices.filter((service) => service.serviceBy === 'withProducts'); if (activeServices.length > 0) { let flag = true; for (let index = 0; index < items.length; index++) { let item = items[index]; let isProductMappedToActiveService = activeServices.find( (service) => service.productId === item.product_id.toString() && service?.variants.includes(item.variant_id.toString()) && (!service.serviceBy || service.serviceBy === "withProducts") && !service?.makeBookeasyBtnOptional && !item.properties?.['_bookeasy-booking-id'] ); if (isProductMappedToActiveService) { flag = false; productWithServiceFound = true; //Cart update will resolve by giving updated cart details cartUpdate({ line: index + 1, quantity: 0 }).then((updatedCartDetails) => { document.dispatchEvent(new Event('bookeasy-cart-updated', { cartDetails: updatedCartDetails })); processCartItems(updatedCartDetails, availableServices, productWithServiceFound); }).catch((error) => { console.log("Error", error); }); break; } } //If the product is mapped to a service and the cart is updated, then reload the page. If no product is mapped to a service, then it will not reload if (flag && productWithServiceFound) { document.dispatchEvent(new Event('bookeasy-cart-reload', { productWithServiceFound: productWithServiceFound })); window.location.reload(); } } } function registerNetworkListeners() { let cartUpdateUrls = ["/cart/add.js", "/cart/add"]; let isScriptCall = false; // XMLHttpRequest interceptor var bookeasyCartValidationXMLRequestListeners = window.XMLHttpRequest.prototype.open; function openReplacement(method, url, async, user, password) { // Check if the call is from our own scripts const stackTrace = new Error().stack; if (stackTrace.includes('processCartItems') || stackTrace.includes('cartUpdate')) { isScriptCall = true; } else { isScriptCall = false; } this.addEventListener("load", async function () { if (!isScriptCall && typeof url === "string") { let splitURL = url.split("?"); if (splitURL && splitURL.length) { if (cartUpdateUrls.includes(splitURL[0])) { processCartItems(undefined, undefined, productWithServiceFound); } } } }); return bookeasyCartValidationXMLRequestListeners.apply(this, arguments); } window.XMLHttpRequest.prototype.open = openReplacement; // Fetch interceptor var bookeasyCartValidationFetchRequestListeners = fetch; fetch = async function (url, options) { // Check if the call is from our own scripts const stackTrace = new Error().stack; if (stackTrace.includes('processCartItems') || stackTrace.includes('cartUpdate')) { isScriptCall = true; } else { isScriptCall = false; } var promise = bookeasyCartValidationFetchRequestListeners(url, options); await promise; if (!isScriptCall && typeof url === "string") { let splitURL = url.split("?"); if (splitURL && splitURL.length) { if (cartUpdateUrls.includes(splitURL[0])) { processCartItems(undefined, undefined, productWithServiceFound); } } } return promise; } } function DOMcontentLoadListener() { if (document.readyState === 'complete' || document.readyState === 'interactive') { if (processCart) { processCart = false processCartItems(undefined, undefined, productWithServiceFound); } } else { document.addEventListener("readystatechange", () => { if (processCart && (document.readyState === 'complete' || document.readyState === 'interactive')) { processCart = false; processCartItems(undefined, undefined, productWithServiceFound); } }); document.addEventListener('DOMContentLoaded', function () { if (processCart) { processCart = false; processCartItems(undefined, undefined, productWithServiceFound); } }); } } function isPrivateTab() { try { let storage = typeof localStorage; return false; } catch (error) { return true; } } if (!isPrivateTab() && localStorage.getItem('bookeasy-setup')) { let bookeasySetup = JSON.parse(localStorage.getItem('bookeasy-setup')); if (!bookeasySetup?.stopAppEmbedCartValidation) { registerNetworkListeners(); DOMcontentLoadListener(); } } else { registerNetworkListeners(); DOMcontentLoadListener(); }