(function (window) { setTimeout(function () { function toFraction(value) { return parseFloat((value / 100).toFixed(2)); } vtexjs.checkout.getOrderForm().done(function (orderForm) { // console.log("This getOrderForm is responsible to initialize insider_obejct and trigger orderFormUpdated.vtex event") window.insider_object = { basket: { line_items: [], currency: orderForm.storePreferencesData.currencyCode, total: null }, page: { type: "Checkout" }, user: {} }; updateUserInfo(orderForm); updateProducts(orderForm); updatePageType(); }); $(window).on('orderFormUpdated.vtex', function (event, orderForm) { updateUserInfo(orderForm); updateProducts(orderForm); }); // Responsible to change current page type window.addEventListener("hashchange", () => { updatePageType() }, false); const updatePageType = function () { if (window.location.hash === "#/cart") { if (window.insider_object && window.insider_object.page) { window.insider_object.page.type = "Basket"; } } else { if (window.insider_object && window.insider_object.page) { window.insider_object.page.type = "Checkout"; } } }; // Responsible to fill the user data into insider_object const updateUserInfo = async function (orderForm) { if (!orderForm.clientProfileData?.email || !window.insider_object) return; if (!orderForm.canEditData) { window.insider_object.user = { email: orderForm.clientProfileData.email }; return; } const customer = await getCustomer(orderForm); if (window.insider_object && customer.gdpr_optin) { window.insider_object.user = { ...customer, name: orderForm.clientProfileData.firstName, surname: orderForm.clientProfileData.lastName, phone_number: orderForm.clientProfileData.phone, email: orderForm.clientProfileData.email, email_optin: orderForm.clientPreferencesData?.optinNewsLetter, language: orderForm.clientPreferencesData?.locale, }; } else { window.insider_object.user = { uuid: null, name: orderForm.clientProfileData.firstName, surname: orderForm.clientProfileData.lastName, phone_number: orderForm.clientProfileData.phone, email: orderForm.clientProfileData.email, email_optin: orderForm.clientPreferencesData?.optinNewsLetter, language: orderForm.clientPreferencesData?.locale, }; } }; const getCustomer = async (orderForm) => { const authUserResponse = await fetch('/insider/get-user-data?userId=' + orderForm.userProfileId); if (authUserResponse.status !== 200) { return { uuid: null, has_transacted: false, return: true, transaction_count: 0, gdpr_optin: false }; }; const authUser = await authUserResponse.json(); return authUser; }; const updateProducts = (orderForm) => { if (!window.insider_object) return; if (window.insider_object.basket.total === toFraction(orderForm.value)) return; window.insider_object.basket.line_items = []; orderForm.items.forEach(function (item) { // Need to fetch products because we need to get the product and product image link fetch('/api/catalog_system/pub/products/search?fq=productId:' + item.productId).then(res => res.json()).then((res) => { const itemFind = res[0].items.find((i) => i.itemId === item.id); const sellerFind = itemFind.sellers.find((s) => s.sellerDefault); const data = { product: { id: item.id, name: item.name, taxonomy: res[0].categories.map(category => category.split('/').join('')), unit_price: toFraction(item.price), unit_sale_price: toFraction(item.sellingPrice), currency: orderForm.storePreferencesData.currencyCode, url: res[0].link, product_image_url: itemFind.images[0].imageUrl, stock: sellerFind.commertialOffer.AvailableQuantity, groupcode: res[0].productId, custom: { brand: res[0].brand, brandId: res[0].brandId, categoryId: res[0].categoryId, linkText: res[0].linkText, productReference: res[0].productReference, taxonomy: res[0].categories.map(category => category.split('/').join('')), unit_price: toFraction(item.price), unit_sale_price: toFraction(item.sellingPrice), } }, quantity: item.quantity, subtotal: toFraction(item.sellingPrice * item.quantity), } window.insider_object.basket.line_items.push(data); }); }); window.insider_object.basket.total = toFraction(orderForm.value); }; // Responsible to export to window a function that can insert custom attributes const insertCustomAttrInsider = (subObj, customFields) => { window.insider_object = window.insider_object || {} let insider_object = window.insider_object let new_obj = { ...insider_object, [subObj]: { ...insider_object[subObj], ...customFields } } window.insider_object = new_obj } window.insertCustomAttrInsider = insertCustomAttrInsider; }, 2000); const insiderSwSdkScript = document.createElement("script"); insiderSwSdkScript.type = "text/javascript"; insiderSwSdkScript.src = "/files/insider-sw-sdk.js"; document.body.appendChild(insiderSwSdkScript); const insiderScript = document.createElement("script"); insiderScript.type = "text/javascript"; insiderScript.src = "//totto.api.useinsider.com/ins.js?id=10009667"; document.body.appendChild(insiderScript); const insiderServiceWorkerScript = document.createElement("script"); insiderServiceWorkerScript.type = "text/javascript" insiderServiceWorkerScript.src = "https://api.useinsider.com/sw.js" document.body.appendChild(insiderServiceWorkerScript); })(window);