ACDiscountApp.PDPage.DisplayTiers = function () { var tabelSelectorPDpage = '.product-form .payment-and-quantity'; // Tiered Table Selector; var tabelQuantitySelectorPDpage = '.product-form .payment-and-quantity input[name="quantity"]';// add quantity box selecter var addToCartSelecter = '.product-form .payment-and-quantity';// add t cart button selecter var VarintChangeSelectorPDpage = '.product-details .option-selectors .selector-wrapper select,.product-details .payment-and-quantity__add'; var storeTime = globalFields.Storetime(); // Parse the date string into individual components let [currentYear, currentMonth, currentDay, currentHours, currentMinutes, currentSeconds] = storeTime.split(/[\sT:-]/); currentYear = parseInt(currentYear); currentMonth = parseInt(currentMonth); // Months are zero-based in JavaScript currentDay = parseInt(currentDay); currentHours = parseInt(currentHours); currentMinutes = parseInt(currentMinutes); currentSeconds = parseInt(currentSeconds); function makeTimer(end_date) { if (globalFields.isProduct_Page) { if (end_date != null) { jQuery("#ac_countDown").css("display", "block"); var endTime = end_date; // Increment seconds by 1 currentSeconds++; // Adjust minutes, hours, and day if necessary if (currentSeconds >= 60) { currentSeconds = 0; currentMinutes++; if (currentMinutes >= 60) { currentMinutes = 0; currentHours++; if (currentHours >= 24) { currentHours = 0; currentDay++; } } } // Reformat the updated timestamp let currentStoreTime = `${currentYear}-${currentMonth.toString().padStart(2, '0')}-${currentDay.toString().padStart(2, '0')}T${currentHours.toString().padStart(2, '0')}:${currentMinutes.toString().padStart(2, '0')}:${currentSeconds.toString().padStart(2, '0')}`; endTime = (Date.parse(endTime) / 1000); var now = currentStoreTime; now = (Date.parse(now) / 1000); var timeLeft = endTime - now; var days = Math.floor(timeLeft / 86400); var hours = Math.floor(timeLeft / 3600); var exactHour = hours % 24; var minutes = Math.floor((timeLeft - (hours * 3600)) / 60); var seconds = Math.floor((timeLeft - (hours * 3600) - (minutes * 60))); var isDays = false; if (days > 0 || hours > 24) { isDays = true; } if (days < 0) { days = "00"; /*hours = "0"; minutes = "0"; seconds = "0";*/ } if (hours < 10) { hours = "0" + hours; } if (minutes < 10) { minutes = "0" + minutes; } if (seconds < 10) { seconds = "0" + seconds; } if (timeLeft > 0) { if (isDays) { $(".ac__days").html(days); $(".ac__hours").html(exactHour); $(".ac__minutes").html(minutes); $(".ac_sec, .ac__colon.seconds").hide(); } else { $(".ac__hours").html(exactHour); $(".ac__minutes").html(minutes); $(".ac__seconds").html(seconds); $(".ac_day, .ac__colon.hours").hide(); $(".ac_sec, .ac__colon.seconds").show(); } } else { jQuery('.ac__settings__table, #ac_countDown').remove(); } setTimeout(function () { jQuery('.ac__settings__table').after(jQuery("#ac_countDown")); }, 500); } } } var hide_buttons = null; var HideAdditionalPayments = function () { hide_buttons = setInterval(function () { if (jQuery('.shopify-payment-button__button').length > 0) { jQuery('.shopify-payment-button__button').hide(); clearInterval(hide_buttons); } }, 500); } this.DisplayTiersFn = function () { setTimeout(function () { jQuery('.ac__settings__table').remove(); var selectedVariant = jQuery('input[name^=id]:checked, select[name^=id], input[name=id], hidden[name^=id]', jQuery('form[action="/cart/add"]')).val(); let ProductPage = globalFields.TableProductPage != undefined && globalFields.TableProductPage != '' ? globalFields.TableProductPage : 'true'; ProductPage = ProductPage == 'true' ? true : false; if(selectedVariant && selectedVariant != null && (globalFields.custom_price_status == 'true' || globalFields.custom_price_status )){setProductCustom_prices(selectedVariant) } if (selectedVariant != null && globalFieldsProductPage_AC.productMetafield != '[]' && globalFieldsProductPage_AC.productMetafield != undefined && ProductPage) { let productMetafield = Array.isArray(globalFieldsProductPage_AC.productMetafield) ?filterDiscountTier(selectedVariant, globalFieldsProductPage_AC.productMetafield) :filterDiscountTier(selectedVariant, [globalFieldsProductPage_AC.productMetafield]) let tierObj = productMetafield && globalFields.GetTierObject(productMetafield, selectedVariant); if (!tierObj) { productMetafield = globalFieldsProductPage_AC.AllProductTier != "[]" && filterDiscountTier(selectedVariant, globalFieldsProductPage_AC.AllProductTier) tierObj = productMetafield && globalFields.GetTierObject(productMetafield, selectedVariant); } if (!tierObj) {return } let DiscountAppliedOn = false; let Location_Tag_s = false; if (tierObj != undefined) { DiscountAppliedOn = tierObj.DiscountAppliedON != null && tierObj.DiscountAppliedON != '' && tierObj.DiscountAppliedON != undefined ? true : false; Location_Tag_s = tierObj.Location_Tag_ != null && tierObj.Location_Tag_ != '' && tierObj.Location_Tag_ != undefined ? true : false; } if (DiscountAppliedOn) { // 1 if (Location_Tag_s) { // 2 let DiscountAppliedOnTag = tierObj.DiscountAppliedON == 'Both_Store' || tierObj.DiscountAppliedON == 'Online_Store' ? true : false; let LocationObject = Object.keys(tierObj.Location_Tag_); if (LocationObject[0] == 'StoreLocation') { let StoreLocationcheck = tierObj.Location_Tag_[LocationObject[0]].includes(globalFields.Storecountry) || tierObj.Location_Tag_[LocationObject[0]].includes(globalFields.Storecity) ? true : false; if (tierObj != undefined && tierObj.status && globalFields.StartEndDateValid(tierObj.start_date, tierObj.end_date) && DiscountAppliedOnTag && StoreLocationcheck) { var getDates = globalFields.TimerStartEndDateValid(tierObj.start_date, tierObj.end_date); getDates.valid; getDates.start_date; getDates.end_date; let timer_settings = parsed_timer_settings; WriteTableHeading(tierObj); WriteTableRows(tierObj, selectedVariant); if (timer_settings != undefined) { if (timer_settings.TimerStatus) { if (getDates.valid) { setInterval(function () { makeTimer(getDates.end_date); }, 1000); } } } jQuery('.shopify-payment-button__button').hide(); setTimeout(function () { jQuery('.shopify-payment-button__button').hide(); HideAdditionalPayments(); }, 2000); setTimeout(function () { clearInterval(hide_buttons); }, 5000); } } else { // 3 let TierLocation = tierObj.Location_Tag_[LocationObject[0]]; let CusotmerCountryLocation = globalFields.Customercountry; let CusotmerCityLocation = globalFields.Customercity; let CustomerLocationcheck_1 = (arr, arr1) => { return arr.some(item => arr1.includes(item)) }; let CustomerLocationcheck_2 = (arr2, arr3) => { return arr2.some(item => arr3.includes(item)) }; let CityValue = CustomerLocationcheck_1(TierLocation, CusotmerCountryLocation); let CountryValue = CustomerLocationcheck_2(TierLocation, CusotmerCityLocation); if (tierObj != undefined && tierObj.status && globalFields.StartEndDateValid(tierObj.start_date, tierObj.end_date) && DiscountAppliedOnTag && (CityValue || CountryValue)) { var getDates = globalFields.TimerStartEndDateValid(tierObj.start_date, tierObj.end_date); getDates.valid; getDates.start_date; getDates.end_date; let timer_settings = parsed_timer_settings; WriteTableHeading(tierObj); WriteTableRows(tierObj, selectedVariant); if (timer_settings != undefined) { if (timer_settings.TimerStatus) { if (getDates.valid) { setInterval(function () { makeTimer(getDates.end_date); }, 1000); } } } jQuery('.shopify-payment-button__button').hide(); setTimeout(function () { jQuery('.shopify-payment-button__button').hide(); HideAdditionalPayments(); }, 2000); setTimeout(function () { clearInterval(hide_buttons); }, 5000); } } } else { // 4 if (tierObj != undefined && tierObj.status && globalFields.StartEndDateValid(tierObj.start_date, tierObj.end_date) && (tierObj.DiscountAppliedON == 'Both_Store' || tierObj.DiscountAppliedON == 'Online_Store')) { var getDates = globalFields.TimerStartEndDateValid(tierObj.start_date, tierObj.end_date); getDates.valid; getDates.start_date; getDates.end_date; let timer_settings = undefined//parsed_timer_settings; WriteTableHeading(tierObj); WriteTableRows(tierObj, selectedVariant); if (timer_settings != undefined) { if (timer_settings.TimerStatus) { if (getDates.valid) { setInterval(function () { makeTimer(getDates.end_date); }, 1000); } } } jQuery('.shopify-payment-button__button').hide(); setTimeout(function () { jQuery('.shopify-payment-button__button').hide(); HideAdditionalPayments(); }, 2000); setTimeout(function () { clearInterval(hide_buttons); }, 5000); } } } else { // 5 let temptimer = globalFields.StartEndDateValid(tierObj.start_date, tierObj.end_date) if (tierObj != undefined && tierObj.status && globalFields.StartEndDateValid(tierObj.start_date, tierObj.end_date)) { var getDates = globalFields.TimerStartEndDateValid(tierObj.start_date, tierObj.end_date); getDates.valid; getDates.start_date; getDates.end_date; let timer_settings = undefined//parsed_timer_settings; WriteTableHeading(tierObj); WriteTableRows(tierObj, selectedVariant); if (timer_settings != undefined) { if (timer_settings.TimerStatus) { if (getDates.valid) { setInterval(function () { makeTimer(getDates.end_date); }, 1000); } } } jQuery('.shopify-payment-button__button').hide(); setTimeout(function () { jQuery('.shopify-payment-button__button').hide(); HideAdditionalPayments(); }, 2000); setTimeout(function () { clearInterval(hide_buttons); }, 5000); } } } }, 200); } setTimeout(function () { clearInterval(hide_buttons); }, 10000); this.OnVariantChange = function () { /* setTimeout(function () { $(VarintChangeSelectorPDpage).click(function () { displayTiers.DisplayTiersFn(); }); }, 2000); */ } $(document).on('click', VarintChangeSelectorPDpage, function () { setTimeout(displayTiers.DisplayTiersFn, 1500); setTimeout(displayTiers.DisplayTiersFn, 2500); setTimeout(displayTiers.DisplayTiersFn, 3500); setTimeout(displayTiers.DisplayTiersFn, 4500); }); var TableHeadHtml = function (contentHtml, tableSelector, position) { let tableShowAfter = false; jQuery(tableSelector)[globalFields.settings.table_body.table_placed == 'after' ? 'after' : 'before'](contentHtml); } var WriteTableHeading = function (tierObj) { var tableSelector = tabelSelectorPDpage; if (globalFields.settings.selected_table == "table1") { TableHeadHtml('
' + globalFields.settings.table_headers.header1_value + '' + globalFields.settings.table_headers.header2_value + '
', tableSelector); } if (globalFields.settings.selected_table == "table2") { TableHeadHtml('
' + globalFields.settings.table_headers.header1_value + '' + globalFields.settings.table_headers.header2_value + '' + globalFields.settings.table_headers.header3_value + '
', tableSelector); } if (globalFields.settings.selected_table == "table3") { TableHeadHtml('
' + globalFields.settings.table_headers.header1_value + '' + globalFields.settings.table_headers.header2_value + '' + globalFields.settings.table_headers.header3_value + '
', tableSelector); } if (globalFields.settings.selected_table == "table4") { TableHeadHtml('
', tableSelector); } if (globalFields.settings.selected_table == "table5") { TableHeadHtml('
',tableSelector); } } var TableRowHtml = function (contentHtml, tableSelector) { jQuery(tableSelector).append(contentHtml); } function formatNumberWithCommas(number) { if (typeof number !== 'number') { return number; } return number; } var TableRowSingle = function (tieredMin_, tieredMax_, discountedPrice, tieredOff,mostPopularValues, index,originalPrice_,lastLength) { let mostPopular = mostPopularValues ? `
Most Popular` : ''; var plusSign = tieredMin_ == tieredMax_ ? '' : '+', buyText = 'Buy '; var priceHtml = '' + globalFields.formatMoney(discountedPrice, globalFields.amount) + ''; if (globalFields.settings.selected_table == "table1") { TableRowHtml(' ' + tieredMin_ + plusSign + mostPopular + '' + tieredOff + '', '#ac_table1 .ac__tbody'); } if (globalFields.settings.selected_table == "table2") { TableRowHtml(' ' + tieredMin_ + mostPopular + '' + tieredMax_ + '' + tieredOff + '', '#ac_table2 .ac__tbody'); } if (globalFields.settings.selected_table == "table3") { TableRowHtml(' '+ globalFields.settings.table_body.body1_value +' ' + tieredMin_ + plusSign + mostPopular + '' + priceHtml + '' + tieredOff + '', '#ac_table3 .ac__tbody'); } if (globalFields.settings.selected_table == "table4") { var quantityTextTable4 = tieredMin_ + "-" + tieredMax_; if (tieredMax_ == "+") { quantityTextTable4 = tieredMin_ + "+"; } TableRowHtml(' ' + globalFields.settings.table_body.body1_value + ' ' + tieredMin_ + ' ' + globalFields.settings.table_body.body2_value + ' ' + tieredOff + '', '#ac_table4'); } if (globalFields.settings.selected_table == "table5") { let grabThisdealBtn = lastLength == index ? ``: "" ; TableRowHtml('
'+ globalFields.settings.table_body.body1_value +' '+ formatNumberWithCommas(tieredMin_) +' ' + globalFields.settings.table_headers.header2_value + ' '+ tieredOff + ' ' + globalFields.settings.table_headers.header3_value + ''+mostPopular+'
'+ tieredOff + ' '+globalFields.formatMoney(originalPrice_*100, globalFields.amount)+'
'+ globalFields.settings.table_headers.header1_value +'
' +globalFields.formatMoney( (tieredMin_ * originalPrice_).toFixed(2) , globalFields.amount) +' ' + globalFields.formatMoney((tieredMin_ *Number(discountedPrice)).toFixed(2) , globalFields.amount) +'
'+ grabThisdealBtn +' ', '#ac_table5 .ac__tbody'); if(lastLength == index){GrabThisDeal()} } } var WriteTableRows = function (tierObj, selectedVariant_) { var originalPrice_ = globalFieldsProductPage_AC.variantsPriceArray_PD[selectedVariant_]; originalPrice_ = originalPrice_ / 100; for (i = 0; i < tierObj.tier_min.length; i++) { var isBreakLoop = false, tieredOff = Number(tierObj.tier_values[i]); var tieredMin_ = parseInt(tierObj.tier_min[i]), tieredMax_ = tierObj.tier_max[i], discountedPrice = 0; if (tieredMax_ != "max") { tieredMax_ = parseInt(tieredMax_); } else { tieredMax_ = "+"; } if (tierObj.discount_type == "percentage") { discountedPrice = Number(originalPrice_.toFixed(2)); var p = parseFloat(tieredOff) / 100, originalPriceCut_ = Number(parseFloat(p * discountedPrice).toFixed(2)); discountedPrice = discountedPrice - originalPriceCut_; if (discountedPrice <= 0) { discountedPrice = 0; tieredOff = "100%"; isBreakLoop = true; tieredMax_ = "+"; } else { discountedPrice = discountedPrice.toFixed(2); tieredOff = tieredOff + "% OFF"; } } else if (tierObj.discount_type == "fixed_price") { discountedPrice = tieredOff.toFixed(2); tieredOff = globalFields.formatMoney(tieredOff.toFixed(2), globalFields.amount); } else { discountedPrice = Number((originalPrice_ - tieredOff).toFixed(2)); if (discountedPrice <= 0) { discountedPrice = 0; tieredOff = globalFields.formatMoney(originalPrice_.toFixed(2), globalFields.amount); isBreakLoop = true; tieredMax_ = "+"; } else { discountedPrice = discountedPrice.toFixed(2); tieredOff = globalFields.formatMoney(tieredOff.toFixed(2), globalFields.amount) + " OFF"; } } let lastLength = isBreakLoop ? i + 1 : tierObj.tier_min.length let booleanmostPopularValues = tierObj.mostPopularValues != undefined ? (tierObj.mostPopularValues[i] != undefined ? tierObj.mostPopularValues[i] : false) : false; if(discountedPrice > 0 && Number(originalPrice_) >= Number(discountedPrice)){ TableRowSingle(tieredMin_, tieredMax_, discountedPrice, tieredOff,booleanmostPopularValues, i + 1,originalPrice_,lastLength); } if (isBreakLoop) { break; } } // hide table if discount is greter then product price let tableSize = $(`.ac_${globalFields.settings.selected_table} .ac__tbody tr`).length if(tableSize == 1){$(`.ac_${globalFields.settings.selected_table}`).hide();} } function setProductCustom_prices (selectedVariant_){ var custom_prices = globalFieldsProductPage_AC.custom_prices[selectedVariant_]; custom_prices = custom_prices != "[]" ? custom_prices*Shopify.currency.rate : 0; var originalPrice_ = globalFieldsProductPage_AC.variantsPriceArray_PD[selectedVariant_]; originalPrice_ = originalPrice_ / 100; let productPrice = custom_prices && custom_prices > 0 ? custom_prices : originalPrice_; // jQuery('.product .price__container .price__regular').hide(); //jQuery('.badge.price__badge-sale').hide(); //jQuery('.product-form .product-price').html(`${globalFields.formatMoney(productPrice.toFixed(2), globalFields.amount)} ${Shopify.currency.active}`) //jQuery('.product-form .product-price').html(`${globalFields.formatMoney(productPrice.toFixed(2), globalFields.amount)} ${Shopify.currency.active}`); jQuery('.product-form .product-price').html(`${theme.formatMoney(productPrice.toFixed(2))}`); } var GrabThisDeal = function () { let quantityInputContainer = document.querySelector(tabelQuantitySelectorPDpage) let radioBtnValue = document.querySelector(".ac-quantity-input-box") let acMainContainer = document.querySelector(".ac_main_container") let quantityInputValue = radioBtnValue.value; radioBtnValue.checked = true acMainContainer.classList.add('ac-active') if(quantityInputContainer){ quantityInputContainer.value = quantityInputValue } $(".ac_main_container").on("click", function () { quantityInputContainer = document.querySelector(tabelQuantitySelectorPDpage) quantityInputValue = $(this).find(".ac-quantity-input-box").val(); //quantityInputContainer.value = quantityInputValue // uncomment this if wants to show tier value on the click of ac_main_container $(this).find(".ac-quantity-input-box").prop("checked", true) if (!document.querySelector(".ac-quantity-input-box").checked) { radioBtnValue.style.backgroundColor = ""; acMainContainer.style.backgroundColor = "" //acMainContainer[0].classList.remove('ac-active') } // Toggle 'active' class on containers $(".ac_main_container").removeClass('ac-active'); $(this).addClass('ac-active'); }); // Event listener for grab deal button jQuery('.ac_grab_deal_btn').click(function () { quantityInputContainer = document.querySelector(tabelQuantitySelectorPDpage) if(quantityInputContainer){ quantityInputContainer.value = quantityInputValue } // Optionally trigger the add to cart button if(globalFields.settings.table_body.gtd_add_to_cart){ $(addToCartSelecter).click(); // Uncomment and ensure the selector is correct } }); } } // Get Variant Tier function getVariantTier(variantId,metaArray) { // Define the priority order const priorityOrder = ["Both_Store", "Online_Store"]; // Iterate through the metaArray to find matching objects const matchingObjects = []; for (let index = 0; index < metaArray.length; index++) { const element = metaArray[index]; if(element.entity_type == "variants"){ element.variant_ids.forEach((id, index) => { if (id === variantId && element.entity_type == "variants") { matchingObjects.push({ ...element, matchIndex: index, // Keep track of the index where the match occurred }); } }); } } // Sort the matching objects based on priority matchingObjects.sort((a, b) => { const getPriority = (obj) => { return obj.status[obj.matchIndex] === true ? priorityOrder.indexOf(obj.DiscountAppliedON[obj.matchIndex]) !== -1 ? priorityOrder.indexOf(obj.DiscountAppliedON[obj.matchIndex]) : Infinity : Infinity + 1; // Non-active objects have lower priority }; const aPriority = getPriority(a); const bPriority = getPriority(b); return aPriority - bPriority; }); // Return the highest priority object or null if no matches return matchingObjects.length > 0 ? matchingObjects : null; } // Get Product Tier function getProductTier(data) { const validItems = data.filter(item => item.status); const bothStoreItem = validItems.find(item => item.DiscountAppliedON === "Both_Store"); if (bothStoreItem) return [bothStoreItem]; const onlineStoreItem = validItems.find(item => item.DiscountAppliedON === "Online_Store"); return onlineStoreItem ? [onlineStoreItem] : null; } // filter Variant Product Rule function filterDiscountTier(id,data){ const ruleOne = id ? getVariantTier(id,data) : null; if(!ruleOne){ const ruleTwo = getProductTier(data); return ruleTwo && ruleTwo[0]; } return ruleOne[0]; } var globalFieldsProductPage_AC = new ACDiscountApp.PDPage.Global(), displayTiers = new ACDiscountApp.PDPage.DisplayTiers(); displayTiers.DisplayTiersFn(); displayTiers.OnVariantChange();