var productsData = {}; /*max_ac_products_mobile: 12, max_ac_products_tablet: 10, max_ac_products_desktop: 6,*/ var __fast_options = { with_product_attributes: true, display_popular_on_no_results: true } const specialPrices = {}; const specialComparePrices = {}; const requestedSpecialPrices = {}; function getProductElements(element) { return [...element.querySelectorAll(`.fs-results-product-card`)]; } function productQuickViewButtonClickCallBack(product) { console.log("Product ID: " + product.productId ); console.log("URL: " + product.productURL ); } function setNewPrice(productElement, pId){ let productPrices = specialPrices[pId]; let productcomparePrices = specialComparePrices[pId]; let pPrice = Number(productPrices.p); let cPrice = Number(productcomparePrices.p); if( productElement.querySelector('.fs-serp-price .price, .fs-product-info .fs-price, .quick-view-price-wrapper .quick-view-dialog-price') && productElement.querySelector('.fs-serp-price .price, .fs-product-info .fs-price, .quick-view-price-wrapper .quick-view-dialog-price').textContent.match(/[.,0-9]/gi) && productElement.querySelector('.fs-serp-price .price, .fs-product-info .fs-price, .quick-view-price-wrapper .quick-view-dialog-price').textContent.match(/[.,0-9]/gi)[0]){ let priceElement = productElement.querySelector('.fs-serp-price .price, .fs-product-info .fs-price, .quick-view-price-wrapper .quick-view-dialog-price'); let elPrice = Number(priceElement.textContent.match(/([.,0-9])+/gi)[0].replaceAll(/,/gi,'')); let elId = productElement.getAttribute('data-id') || productElement.getAttribute('data-product-id'); if(pId > 0 && elPrice > 0 && pPrice > 0 && ( pPrice != elPrice || !priceElement.classList.contains('isp-cstm-price-'+pId)) ){ specialPrices[pId] = {p:pPrice}; priceElement.textContent = priceElement.textContent.replace(/([.,0-9])+/gi, pPrice.toFixed(2)); priceElement.classList.add('isp-cstm-price-'+pId); } } if( productElement.querySelector('.compare-container .compare, .quick-view-price-wrapper .quick-view-dialog-compare') && productElement.querySelector('.compare-container .compare, .quick-view-price-wrapper .quick-view-dialog-compare').textContent.match(/[.,0-9]/gi) && productElement.querySelector('.compare-container .compare, .quick-view-price-wrapper .quick-view-dialog-compare').textContent.match(/[.,0-9]/gi)[0]){ let cpriceElement = productElement.querySelector('.compare-container .compare, .quick-view-price-wrapper .quick-view-dialog-compare'); let elcPrice = Number(cpriceElement.textContent.match(/([.,0-9])+/gi)[0].replaceAll(/,/gi,'')); let elcId = productElement.getAttribute('data-id') || productElement.getAttribute('data-product-id'); if(pId > 0 && elcPrice > 0 && cPrice > 0 && ( cPrice != elcPrice || !cpriceElement.classList.contains('isp-cstm-compareprice-'+pId)) ){ specialComparePrices[pId] = {p:cPrice}; cpriceElement.textContent = cpriceElement.textContent.replace(/([.,0-9])+/gi, cPrice.toFixed(2)); cpriceElement.classList.add('isp-cstm-compareprice-'+pId); } } } function setProductSpecialPrice(productElement, pId, productTitle){ if(requestedSpecialPrices && requestedSpecialPrices[pId]){ return; } else{ requestedSpecialPrices[pId] = true; jQuery && jQuery.getJSON( `${productTitle}.js`, function(product) { //window.Shopify.routes.root + `products/${productTitle}.js`, function(product) { let pPrice = 0; let cPrice = 0; let pId = 0; if(product.price){ pPrice = Number(product.price); if(pPrice > 0){ pPrice = (pPrice / 100).toFixed(2) ; } } if(product.compare_at_price){ cPrice = Number(product.compare_at_price); if(cPrice > 0){ cPrice = (cPrice / 100).toFixed(2) ; } } if(product.id){ pId = Number(product.id); } if(typeof specialPrices[pId] === 'undefined'){ specialPrices[pId] = {p:pPrice}; } if(typeof specialComparePrices[pId] === 'undefined'){ specialComparePrices[pId] = {p:cPrice}; } setNewPrice(productElement, pId); }); } } function titleToJsonformat(title){ let formattedTitle = title; formattedTitle = formattedTitle.replaceAll(/[\s\&\,]/gi,'-').replaceAll(/-+/gi,'-'); return formattedTitle; } function PLPhooks() { console.log('PLPhooks'); window.SerpOptions.registerHook("serp-product-grid", ({products, element}) => { console.log('serp-product-grid', 'WishlistKing typeof - ' + typeof window.WishlistKing); for (const productElement of getProductElements(element)) { const productID = productElement.dataset.productId; const data = products[productID]; let productTitle = data.productURL || ''; if (data && data.hasOwnProperty('productURL')) { const url = data.productURL; //Set up special prices if( typeof specialPrices[Number(productID)] !== 'undefined'){ setNewPrice(productElement, productID); } else if(window.Shopify && window.Shopify.routes ){ setTimeout(()=>{ setProductSpecialPrice(productElement, productID, productTitle); },0); } if (!productsData.hasOwnProperty(productID)) { $.ajax({ url: url, beforeSend: function() { productsData[productID] = {}; }, success: function (data) { }, }); } else { // console.log(`${productID} is exists`); } } else { alert(productID); } } }); window.addEventListener("fs-custom-events-quick-view-btn-clicked", (e) => { productQuickViewButtonClickCallBack(e.detail); }); } // execution here if (window.SerpOptions) { PLPhooks(); } else { window.addEventListener('fast-serp-ready', function () { PLPhooks(); }); }