'use strict'; var $body, $html, $document; var spSoldStockShop = 'the-steel-supplements.myshopify.com'; var APP_DOMAIN = '//app.soldstock.io/soldstockapp'; var jQueryUrl = '//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js'; var iconUrl = APP_DOMAIN + '/admin/assets/css/emoticons.css?version=1&shop=' + spSoldStockShop; var soldStockAppSetting, productInStock, productSoldCount, productID, firstVariantID, variantStock, soldappWposition, assignProductType, soldStockSetting, inStockSetting, soldStockDispaly, soldStockType, soldStockIcon, soldStockText, inStockDisplay, inStockText, inStockInline, inStockLimitSetting, inStockLimitApply, inStockLimit; function spLoadAppFile(url, type, callback){ var spLoadElement; if(type == 'style'){ spLoadElement = document.createElement('link'); spLoadElement.rel = "stylesheet"; spLoadElement.type = "text/css"; }else{ spLoadElement = document.createElement('script'); spLoadElement.type = 'text/javascript'; } if(spLoadElement.readyState){ spLoadElement.onreadystatechange = function(){ if(spLoadElement.readyState === 'loaded' || spLoadElement.readyState === 'complete'){ spLoadElement.onreadystatechange = null; callback(); } }; }else{ spLoadElement.onload = function(){ callback(); }; } if(type == 'style'){ spLoadElement.href = url; }else{ spLoadElement.src = url; } document.getElementsByTagName('head')[0].appendChild(spLoadElement); } function spDocumentReady(callback){ /* see if DOM is already available */ if (document.readyState === "complete" || document.readyState === "interactive"){ /* call on next available tick */ setTimeout(callback, 1); }else{ document.addEventListener("DOMContentLoaded", callback); } } function spSalePoupInit(){ if(!window.spSalePopupSetting) return false; var closeTimeout; var salePopupIndex = 0; var salePopupTime = spSalePopupSetting.time; var salePopupDelay = spSalePopupSetting.delay; var salePopupPerPage = spSalePopupSetting.perPage; var salePopupRepeat = spSalePopupSetting.repeat; var salePopupSales = spSalePopupSetting.sales; var salePopupHeading = spSalePopupSetting.heading; var salePopupDisplay = spSalePopupSetting.display; var salePopupPosition = spSalePopupSetting.position; var salePopupColor = spSalePopupSetting.color; var currentDeviceType = getDeviceType(); if(salePopupDisplay == 'disable' || !salePopupDisplay?.includes(currentDeviceType)) return; $.get('/apps/filter?a=popup-products&s='+spSoldStockShop, function(response){ if(typeof response == 'undefined') return; if(response == '') return; response = JSON.parse(response); if(!response.length) return; if(parseInt(salePopupPerPage) <= 0) return; setTimeout(function(){ showSalespopup(); }, salePopupTime * 1000); $('#salsepop_close').on('click', function(){ clearTimeout(closeTimeout); $('.sold_sale_popup').removeClass('sold_sale_show'); if((parseInt(salePopupIndex) + 1) <= salePopupPerPage){ if(typeof response[salePopupIndex] == 'undefined'){ if(salePopupRepeat !== true) return; salePopupIndex = 0; } setTimeout(function(){ showSalespopup(); }, salePopupDelay * 1000); } }); function showSalespopup(){ $('.sold_sale_popup').removeClass('sold_sale_popup_tl sold_sale_popup_tr sold_sale_popup_bl sold_sale_popup_br sold_sale_show'); var productData = response[salePopupIndex]; $('.sold_sale_popup .sold_sale_product_image img').attr('src', productData.image); $('.sold_sale_popup .noti-body a').text(productData.title); $('.sold_sale_popup .noti-body a').attr("href", `https://${spSoldStockShop}/products/${productData.handle}`); $('.sold_sale_popup .noti-title span').text(salePopupHeading); $('.sold_sale_popup .noti-time').text(productData.date); $('.sold_sale_popup .noti-title span, .sold_sale_popup .noti-body a, .sold_sale_popup .noti-time').css({color: salePopupColor}); if(salePopupPosition == 'top-left') $('.sold_sale_popup').addClass('sold_sale_popup_tl'); else if(salePopupPosition == 'top-right') $('.sold_sale_popup').addClass('sold_sale_popup_tr'); else if(salePopupPosition == 'bottom-left') $('.sold_sale_popup').addClass('sold_sale_popup_bl'); else if(salePopupPosition == 'botom-right') $('.sold_sale_popup').addClass('sold_sale_popup_br'); salePopupIndex++; $('.sold_sale_popup').addClass('sold_sale_show'); closeTimeout = setTimeout(function(){ $('#salsepop_close').click(); }, 5000); } }); } function getDeviceType(){ if(navigator.userAgent.match(/mobile|iPad|Android|Touch/i)) return 'mobile'; return 'desktop'; } function spSoldStockAppCollectionInit(){ $document.find('[id^=sold-product-]').each(function(){ var elementID = $(this).attr('id'); var collSoldHtml = generateSoldStockWidget(JSON.parse($(this).data('spSoldStockSetting'))); if(collSoldHtml !== false) $('#'+elementID).html(collSoldHtml); }); } function spSoldStockAppProductInit(){ var proSoldHtml = generateSoldStockWidget(spSoldStockSetting); if($(document).find('.sold_mainbox').length) $(document).find('.sold_mainbox').remove(); if(soldappWposition == "0"){ $("#sold-stock-manual").append(proSoldHtml); } else if(soldappWposition == "2"){ $('form[action="/cart/add').first().after(proSoldHtml); /*$(proSoldHtml).insertAfter('form[action="/cart/add');*/ } else { $('form[action="/cart/add').first().before(proSoldHtml); /*$(proSoldHtml).insertBefore('form[action="/cart/add');*/ } $document.on('change', '.single-option-selector', function(){ if(window.spSoldStockSetting.isTimeout && window.spSoldStockSetting.isTimeout == true){ setTimeout(function(){ variantsOnChange(); }, window.spSoldStockSetting.timeTimeout || 1000); }else{ variantsOnChange(); } }); $document.on('change', '[name^=option]', function(){ fireEventOnchangeVariant(); }); $document.on('change', '.product__variant', function(){ fireEventOnchangeVariant(); }); $document.on('change', '[type^=radio]', function(){ fireEventOnchangeVariant(); }); } function fireEventOnchangeVariant(){ if(window.spSoldStockSetting.isTimeout && window.spSoldStockSetting.isTimeout == true){ setTimeout(function(){ variantsOnChange(); }, window.spSoldStockSetting.timeTimeout || 1000); }else{ variantsOnChange(); } } function variantsOnChange(){ if(soldStockType != 'variant') return false; var variantID = $.trim(getParameterByName("variant")); if(variantID == '') variantID = $document.find('select[name=id]').val(); var variantInStock = 0; if($(document).find('.sold_mainbox').length) $(document).find('.sold_mainbox').remove(); if(variantStock[variantID].inventory != undefined){ var soldStockHtml = ''; variantInStock = variantStock[variantID].inventory; if(inStockLimitApply === true && inStockLimit < variantInStock){ variantInStock = 0; } var variantSoldCount = variantStock[variantID].sold; var varSoldHtml = generateSoldStockHtml(inStockInline, inStockDisplay, variantInStock, inStockText, soldStockDispaly, variantSoldCount, soldStockIcon, soldStockText); if(soldappWposition == "0"){ $("#sold-stock-manual").append(varSoldHtml); } else if(soldappWposition == "2"){ $('form[action="/cart/add').first().after(varSoldHtml); /*$(varSoldHtml).insertAfter('form[action="/cart/add');*/ } else { $('form[action="/cart/add').first().before(varSoldHtml); /*$(varSoldHtml).insertBefore('form[action="/cart/add'); */ } } } function settingVariableInit(settingObject){ soldStockAppSetting = settingObject.setting; productInStock = settingObject.productInStock; productSoldCount = settingObject.productSoldCount; firstVariantID = settingObject.firstVariantID; variantStock = settingObject.variantStock; soldappWposition = settingObject.soldapp_w_position; productID = settingObject.productID; assignProductType = settingObject.assignProductType; soldStockSetting = soldStockAppSetting.soldStock; inStockSetting = soldStockAppSetting.inStock; soldStockDispaly = soldStockSetting.display; soldStockType = soldStockSetting.type; soldStockIcon = soldStockSetting.icon; soldStockText = soldStockSetting.text; inStockDisplay = inStockSetting.display; inStockText = inStockSetting.text; inStockInline = inStockSetting.inline; inStockLimitSetting = inStockSetting.limit; inStockLimitApply = inStockLimitSetting.check; inStockLimit = inStockLimitSetting.number; } function generateSoldStockWidget(settingObject){ settingVariableInit(settingObject); if(soldStockType == 'product'){ var soldStockHtml = ''; if(inStockLimitApply === true && inStockLimit < productInStock){ productInStock = 0; } return generateSoldStockHtml(inStockInline, inStockDisplay, productInStock, inStockText, soldStockDispaly, productSoldCount, soldStockIcon, soldStockText); }else if(soldStockType == 'variant'){ var variantID = $.trim(getParameterByName("variant")); if(variantID == ''){ variantID = firstVariantID; } var variantInStock = 0; if(variantStock[variantID].inventory != undefined){ var soldStockHtml = ''; variantInStock = variantStock[variantID].inventory; if(inStockLimitApply === true && inStockLimit < variantInStock){ variantInStock = 0; } } var variantSoldCount = variantStock[variantID].sold; return generateSoldStockHtml(inStockInline, inStockDisplay, variantInStock, inStockText, soldStockDispaly, variantSoldCount, soldStockIcon, soldStockText); }else{ console.error('Sold Stock App Type Is Invalid!'); return false; } } function spSoldStockAppInit(){ $body = $('body'); $html = $('html'); $document = $(document); if($document.find('.sold_sale_popup').length) spSalePoupInit(); window.spCurrentTemplate = spSoldStockSetting.currentTemplate; if($.inArray(spCurrentTemplate, ['product']) == -1 && !spSoldStockSetting.firstVariantID && !$.isNumeric(spSoldStockSetting.firstVariantID)) return; if(spCurrentTemplate == 'collection'){ spSoldStockAppCollectionInit(); }else{ if(spSoldStockSetting.assignProductType == 'custom'){ $.get('/apps/filter?a=assign-products&s='+spSoldStockShop, function(response){ if(typeof response == 'undefined') return; if($.inArray(parseInt(spSoldStockSetting.productID), JSON.parse(response)) != -1) spSoldStockAppProductInit(); }); }else{ spSoldStockAppProductInit(); } } } function generateSoldStockHtml(stockInline, inStock, inStockQty, inStockText, soldStock, soldStockQty, soldStockIcon, soldStockText){ var stockInlineClass = ''; if(stockInline === true) stockInlineClass = 'inline'; var soldStockHtml = '
'; if(inStock === true && inStockQty > 0){ if(inStockText == undefined || inStockText == ''){ inStockText = 'In Stock'; } soldStockHtml += '' + inStockQty + '  ' + inStockText + ''; } if(stockInline === true && inStock === true && soldStock === true && soldStockQty > 0 && inStockQty > 0){ soldStockHtml += '/'; } if(soldStock === true && soldStockQty > 0){ soldStockHtml += ''; if(soldStockIcon != ''){ soldStockHtml += ' '; } soldStockHtml += '' + soldStockQty + ''; if(soldStockText == undefined || soldStockText == ''){ soldStockText = 'Sold'; } soldStockHtml += ' ' + soldStockText soldStockHtml += ''; } soldStockHtml += '
'; return soldStockHtml; } function getParameterByName(name){ var regex, results; name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } /* App Script Start */ spDocumentReady(function(){ if(window.spSoldStockSetting == undefined || typeof window.spSoldStockSetting != 'object') return; /* Load app icon */ spLoadAppFile(iconUrl, 'style', function(){ /* Check jquery exists */ if(typeof $ == 'undefined' || typeof jQuery == 'undefined'){ /* Load jquery */ spLoadAppFile(jQueryUrl, 'script', function(){ /* Init default app */ spSoldStockAppInit(); }); }else{ /* Init default app */ spSoldStockAppInit(); } }); });