(function () {
var __fast_options = { with_product_attributes: true};
const specialPrices = {};
const specialComparePrices = {};
const requestedSpecialPrices = {};
function setNewPrice(productElement, pId){
let productPrices = specialPrices[pId];
let productcomparePrices = specialComparePrices[pId];
let pPrice = Number(productPrices.p);
let cPrice = Number(productcomparePrices.p);
if(cPrice > 0 && cPrice > pPrice){
if(!productElement.querySelector('.fs-product-price-wrapper').classList.contains('compareExist')){
productElement.querySelector('.fs-product-price-wrapper').classList.add('compareExist');
}
} else{
productElement.querySelector('.fs-product-price-wrapper').classList.remove('compareExist');
}
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)) ){
removeClassCstmEl(priceElement, 'isp-cstm-price');
/*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('.fs-product-price-wrapper .compare')
&& productElement.querySelector('.fs-product-price-wrapper .compare').textContent.match(/[.,0-9]/gi)
&& productElement.querySelector('.fs-product-price-wrapper .compare').textContent.match(/[.,0-9]/gi)[0] ){
let cpriceElement = productElement.querySelector('.fs-product-price-wrapper .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)) ){
removeClassCstmEl(cpriceElement, 'isp-cstm-compareprice');
/*specialComparePrices[pId] = {p:cPrice};*/
cpriceElement.textContent = cpriceElement.textContent.replace(/([.,0-9])+/gi, cPrice.toFixed(2));
cpriceElement.classList.add('isp-cstm-compareprice-'+pId);
cpriceElement.style.display = 'block';
//!productElement.querySelector('.fs-product-price-wrapper').classList.contains('compareExist') && productElement.querySelector('.fs-product-price-wrapper').classList.add('compareExist');
}
else if( pId > 0 && elcPrice > 0 && cPrice === 0 ){
cpriceElement.style.display = 'none';
//productElement.querySelector('.fs-product-price-wrapper').classList.remove('compareExist');
}
}
else if( productElement.querySelector('.fs-product-price-wrapper') &&
productElement.querySelectorAll('.fs-product-price-wrapper .compare').length === 0 &&
cPrice > 0 ){//TODO
if(productElement.querySelector('.fs-product-info .fs-product-price-wrapper .fs-cstm-compare-price') ){
let cstmCpriceElement = productElement.querySelector('.fs-product-info .fs-product-price-wrapper fs-cstm-compare-price');
let elCPrice = Number(cstmCpriceElement.textContent.match(/([.,0-9])+/gi)[0].replaceAll(/,/gi,''));
if( elCPrice > 0 && (elCPrice !== cPrice || cstmCpriceElement.classList.contains('isp-cstm-compareprice-'+pId) ) ){
removeClassCstmEl(cpriceElement, 'isp-cstm-compareprice');
cstmCpriceElement.style.display = 'block';
}
else{
cstmCpriceElement.style.display = 'none';
}
}
else if( productElement.querySelectorAll('.fs-product-info .fs-product-price-wrapper .fs-cstm-compare-price').length === 0 && pPrice < cPrice){
//productElement.querySelector('.fs-product-info .fs-product-price-wrapper').insertAdjacentHTML('afterbegin',`${cPrice.toFixed(2)}`);
}
}
}
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 removeClassCstmEl(el, classString){
Array.from(el.classList).forEach((c,i)=>{
if(c.toLowerCase().indexOf(classString)>-1){
el.classList.remove(el.classList[i]);
}
})
}
function hooks() {
FastAutocomplete.registerHook('render-autocomplete-product-item', ({product, index, element}) => {
let productTitle = product.u || '';
let productID = product.id;
//Set up special prices
if( typeof specialPrices[Number(productID)] !== 'undefined'){
setNewPrice(element, productID);
}
else if(window.Shopify && window.Shopify.routes && productTitle !== '' ){
setTimeout(()=>{ setProductSpecialPrice(element, productID, productTitle); },0);
}
var priceWrapper = element.querySelector('.fs-product-price-wrapper'),
classPriceText = 'price-title';
if (product.p_c === '0.00' && priceWrapper) {
if (!priceWrapper.querySelector(`.${classPriceText}`)) {
priceWrapper.insertAdjacentHTML('afterbegin', `Price`)
}
} else {
if (priceWrapper && priceWrapper.querySelector(`.${classPriceText}`)) {
priceWrapper.querySelector(`.${classPriceText}`).remove();
}
}
});
}
if (window.FastAutocomplete) {
hooks();
} else {
window.addEventListener('fast-autocomplete-ready', function () {
hooks();
});
}
})();