'+ 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 BogoTableRowSingle = function (message,index) {
TableRowHtml(' '+message+'', '#ac_table4');
}
var WriteTableRows = function (tierObj, selectedVariant_) {
var originalPrice_ = globalFieldsProductPage_AC.variantsPriceArray_PD[selectedVariant_]; originalPrice_ = originalPrice_ / 100;
if(!globalFields.bogoDiscountJsonData.length)return
const productId = `gid://shopify/Product/${globalFields.productId}`; // your input product ID
const matchingDiscounts = globalFields.bogoDiscountJsonData
.filter(bogo =>
bogo.status === true &&
bogo.customerBuys.itemData.some(item => item.id === productId)
)
.sort((a, b) => Number(a.customerBuys.buyQuantity) - Number(b.customerBuys.buyQuantity));
const messages = matchingDiscounts.map((bogo,i) => {
const buyQty = bogo.customerBuys.buyQuantity;
const getQty = bogo.customerGets.buyQuantity;
TableRowSingle(buyQty, buyQty, `get ${getQty} `, `Get ${getQty} Bag Free `,false, i,originalPrice_,i);
// return `Buy ${buyQty} bags, get ${getQty} bag${getQty > 1 ? 's' : ''} free`;
});
if(matchingDiscounts.length === 0){
jQuery('.mainDivAnncode').remove();
}
for (let i = 0; i < messages.length; i++) {
BogoTableRowSingle(messages[i],i);
}
/*
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; }
}
let tableSize = $(`.ac_${globalFields.settings.selected_table} .ac__tbody tr`).length
if(tableSize == 1 && globalFields.settings.selected_table != 'table5' && globalFields.settings.selected_table != 'table4' ){$(`.ac_${globalFields.settings.selected_table}`).hide();}
// 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();}
*/
}
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
}
});
}
}
var globalFieldsProductPage_AC = new ACDiscountApp.PDPage.Global(), displayTiers = new ACDiscountApp.PDPage.DisplayTiers();
displayTiers.DisplayTiersFn(); displayTiers.OnVariantChange();