//constants //window.urlBase = "http://dev.laughingravenherbals.com:8081/"; //window.urlBase = "http://www.laughingravenherbals.com/"; window.urlBase = window.location.origin + "/"; //fetching items from server and/or local session storage let savedItems = sessionStorage.getItem('items'); let fetchPromise = null; if(!savedItems) { console.log("No stored items, fetching from server!"); fetchPromise = fetch('items.json') .then(response=>response.json()) .then(itemsJSON => { let itemsString = JSON.stringify(itemsJSON); sessionStorage.setItem('items', itemsString); return itemsString; }); } else { console.log("Using stored items"); fetchPromise = Promise.resolve(savedItems); } fetchPromise = fetchPromise.then(itemsString => JSON.parse(itemsString)); //global number formatter let formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }); //updating cart information let allCookies = document.cookie; console.log(`allCookies: ${allCookies}`); let getCartContents = () => { let allCookies = document.cookie; let regex = /cart=(.*);/; let result = allCookies.match(regex); let cart = {}; if(result && result.length > 1) { try { cart = JSON.parse(result[1]); } catch (e) { console.log("couldn't parse weird cookie: "+result[1]); } } return cart; }; let setCartContents = (contentsObject) => { document.cookie = `cart=${JSON.stringify(contentsObject)}`; document.cookie = `max-age=${60*60*24*7}`; //a week }; let getCartTotals = () => { let cart = getCartContents(); //{..} let itemsSubtotal = 0; let itemCount = 0; let numSmallItems = 0; let numLargeItems = 0; for(let fullSKU in cart) { let cartItem = cart[fullSKU]; itemsSubtotal += (cartItem.quantity * cartItem.price); console.log(cart); if(cartItem.sku.startsWith('ext-')) { if(cartItem.size.startsWith('8')) { numLargeItems += cartItem.quantity; } else { numSmallItems += cartItem.quantity; } } else if(cartItem.sku.startsWith('bb-') || cartItem.sku.startsWith('oil-')) { numSmallItems += cartItem.quantity; } else { //soaps are big let regex = /(\d) \w*/m; let groupSize = 1; try { let matches = cartItem.size.match(regex); //["3 bars", "3"] groupSize = Number(matches[1]); } catch (e) { console.error("Problem parsing item description for group size"); console.log(e); } numLargeItems += groupSize*cartItem.quantity; // } else if(cartItem.sku.startsWith('bb-')) { // if(cartItem.size.startsWith('1')) { // numSmallItems += cartItem.quantity; // } else { // numSmallItems += 6 * cartItem.quantity; //six bath bombs per unit // } // } else if(cartItem.sku.startsWith('soap-')) { // if(cartItem.size.startsWith('1')) { // numSmallItems += cartItem.quantity; // } else if(cartItem.size.startsWith('3')) { // numSmallItems += 3 * cartItem.quantity; // } else { // numSmallItems += cartItem.quantity; // } // } // } else { // numSmallItems += cartItem.quantity; } } itemCount = numSmallItems + numLargeItems; //calculating shipping right now is just a guess of how many items that I can fit per box //I can fit about 5 small items in a USPS flat rate small envelop or small box ($10 each) //and 10 large (8oz bottle) items in a USPS priority box A which is about $20 let shippingSubtotal = 0; if(numLargeItems > 0) { shippingSubtotal = 18*(1 + Math.floor(itemCount/10)); //10 items per $18 box } else { if(itemCount == 0) shippingSubtotal = 0; else if(itemCount < 5) shippingSubtotal = 10; //small box else if(itemCount < 15) shippingSubtotal = 18; //medium box else if(itemCount < 36) shippingSubtotal = 25; //large flat rate box else shippingSubtotal = 18*(1 + Math.floor(itemCount/18)); //15 items per $18 box } console.log(`shipping calculation, numLargeItems: ${numLargeItems}, numSmallItems: ${numSmallItems}, itemCount: ${itemCount}`) return {itemsSubtotal:itemsSubtotal, shippingSubtotal:shippingSubtotal, total:(itemsSubtotal + shippingSubtotal)}; }; let updateCartBadge = () => { let cart = getCartContents(); console.log(`cart length: ${Object.keys(cart).length}`); if (Object.keys(cart).length > 0) { let cartBadgeDiv = document.getElementById('cart-badge'); if (cartBadgeDiv) { let numItems = 0; for(let fullSKU in cart) { let cartItem = cart[fullSKU]; numItems += Number(cartItem.quantity); } cartBadgeDiv.classList.remove('hidden'); cartBadgeDiv.innerHTML = numItems; } } }; document.addEventListener('DOMContentLoaded', (e) => { updateCartBadge(); });