$(function () { if (opts.cart_ok !== 0) { $.removeCookie('cart_uid', { path: '/', domain: cookieDomain }); location.reload(); return; } var scrollLoad = false; var userLanguage = opts.lang; var userCurrency = opts.curr; var typeCurrency = userCurrency; function showLoading() { scrollLoad = true; $("#items-not-found").addClass("d-none"); } function hideLoading() { scrollLoad = false; $("#skeleton-loading").addClass("d-none") } function showItemLoader(ev) { let product = $(ev.target).closest("[product_id]"); let loader = '' product.find('[name="item-counter"]').before(loader) product.find('[name="item-counter-plus"]').prop("disabled", true) product.find('[name="item-counter-minus"]').prop("disabled", true) product.find('[name="item-delete"]').prop("disabled", true) } function hideItemLoader(ev) { let product = $(ev.target).closest("[product_id]"); product.find("span.loader").remove() product.find('[name="item-counter-plus"]').prop("disabled", false) product.find('[name="item-delete"]').prop("disabled", false) if (product.find('[name="item-counter"]').val() > 1) { product.find('[name="item-counter-minus"]').prop("disabled", false) } } function loadCartProducts(curr) { let request = { "action": 'cart_json', "mode": 1, "cart_curr": curr } showLoading(); return $.get('/asp/ajax.asp', request) .done(function (response) { hideLoading(); parsedResult = JSON.parse(response.html); if (parsedResult.CntGoods > 0) { typeCurrency = parsedResult.TypeCurrency || userCurrency; showResultOnPage(parsedResult); } else { showEmptyResult(); } }) .fail(function () { alert(opts.i18n.something_went_wrong) hideLoading(); }); } function showResultOnPage(data) { $("#deleteAllButton").removeClass("d-none"); $('[name="purchase"]').removeClass("d-none"); $('[name="purchase"] [data-loading]').removeAttr("data-loading") $('[name="purchase"] button').removeAttr("disabled") let available = data.items.filter(x => x.AvailableForPay) let notAvailable = data.items.filter(x => !x.AvailableForPay) if (available.length > 0) { $("#cart-items").append(available.map(x => generateHtmlForProduct(x, typeCurrency))) } if (notAvailable.length > 0) { let divider = `

${opts.i18n.out_of_stock}

` $("#cart-items").append(divider) $("#cart-items").append(notAvailable.map(x => generateHtmlForProduct(x, typeCurrency))) } let itemsCount = GetItemsCountText(data.items.reduce((sum, item) => sum + item.Cnt_Item, 0)); $('[name="cart-items-count"]').text(itemsCount) let currencySymbol = getCurrencySymbol(typeCurrency) let totalPrice = (formatNumber(available.reduce((partialSum, a) => partialSum + a.Amount_Total, 0))); let priceText = `${totalPrice} ${currencySymbol}` $('[name="total-price"]').text(priceText) let productIds = data.items.map(x => parseInt(x.ID_Item)); $.post(`/asp/products_in_favorites.asp`, JSON.stringify([...new Set(productIds)])) .done(function (response) { let productsInFavorites = JSON.parse(response); productsInFavorites.forEach(x => { $(`#cart-items [product_id=${x}] button[action="toggleInFavorites"]`).addClass('button--active') }) }) } function generateHtmlForProduct(item, currency) { let template = document.getElementById("cart-item"); let itemNode = template.content.cloneNode(true); $(itemNode).find('[product_id]').attr("product_id", item.ID_Item).attr("cart_id_number", item.ID_Num); $(itemNode).find('[name="item-seller-name"]').text(item.SellerName); $(itemNode).find('[name="seller-link"]').attr("href", `/seller/${(item.SellerName.replace(/[^a-zA-Z0-9]/g, '-'))}/${item.SellerId}`); $(itemNode).find('[name="item-image"]').attr("src", `//${opts.graphDomain}/img.ashx?id_d=${item.ID_Item}&w=400&h=400`); let productLink = `/itm/${item.Name_Translit}/${item.ID_Item}`; $(itemNode).find('div.card-image-wrapper:has([name="item-image"])').on("click", function () { window.location.href = productLink; }) $(itemNode).find('[name="item-description-and-options"] > a > span').text(RemoveEmoji(item.Name_Item)); $(itemNode).find('[name="item-description-and-options"] > a').attr("href", productLink); if (item.Name_Units) { const nameUnitsParts = item.Name_Units.split(' '); nameUnitsParts[0] = nameUnitsParts[0].replace('.00', ''); let nameUnits = nameUnitsParts.join(' '); let nameUnit = `

${opts.i18n.willget} ${nameUnits}

` $(itemNode).find('[name="item-description-and-options"]').append(nameUnit) } if (item.product_options) { let options = item.product_options.map(x => `

${x.Option_Name} ${(x.Option_Type == "text" || x.Option_Type == "textarea" ? x.User_Data : x.Variant_Name)}

`) $(itemNode).find('[name="item-description-and-options"]').append(options) } $(itemNode).find('[name="item-counter"]').val(item.Cnt_Item); if (item.Cnt_Item > 99) { setCounterFieldStyle($(itemNode).find('[name="item-counter"]'), item.Cnt_Item) } if (item.AvailableForPay) { let currencySymbol = getCurrencySymbol(currency) let price = formatNumber(item.Amount_Total); let priceText = `${price} ${currencySymbol}` $(itemNode).find('[name="item-price"]').text(priceText) if (item.Cnt_Item > 1) { $(itemNode).find('[name="item-counter-minus"]').prop("disabled", false); } $(itemNode).find('[product_id]').attr("amount_item", item.Amount_Item); if (item.Cnt_Lock) { $(itemNode).find('[name="item-counter-minus"]').closest('div').addClass("d-none"); } } else { $(itemNode).find('[name="item-counter-minus"]').remove() $(itemNode).find('[name="item-counter"]').remove() $(itemNode).find('[name="item-counter-plus"]').remove() $(itemNode).find('[name="item-price"]').text(opts.i18n.out_of_stock) } return itemNode; } function showEmptyResult() { $("#items-not-found").removeClass("d-none"); $('[name="purchase"]').addClass("d-none"); } function setCounterFieldStyle(counter, newValue) { if (newValue > 99) { $(counter).css("width", "60px") } else { $(counter).css("width", "50px") } } function increaseProductCount(ev) { let product = $(ev.target).closest("[product_id]"); let counter = product.find('[name="item-counter"]'); let currentValue = parseInt(counter.val()); let newValue = currentValue + 1; showItemLoader(ev) setCounterFieldStyle(counter, newValue) changeCount2(product.attr("cart_id_number"), newValue, userLanguage) .then(function (data) { productData = data.products.filter(x => x.id == product.attr("product_id"))[0] if (productData) { newValue = productData.cnt_item counter.val(newValue); if (productData.error) { ShowWarningNotification(productData.error) } }; let newAmount = parseFloat(product.attr("amount_item")) * newValue let priceText = `${(formatNumber(newAmount))} ${getCurrencySymbol(typeCurrency)}` product.find('[name="item-price"]').text(priceText) if (newValue > 1) { product.find('[name="item-counter-minus"]').prop("disabled", false) } recalculateTotalPrice() recalculateTotalItems() hideItemLoader(ev) }) } function decreaseProductCount(ev) { let product = $(ev.target).closest("[product_id]"); let counter = product.find('[name="item-counter"]'); let currentValue = parseInt(counter.val()); let newValue = Math.max(currentValue - 1, 1); showItemLoader(ev) setCounterFieldStyle(counter, newValue) changeCount2(product.attr("cart_id_number"), newValue, userLanguage) .then(function (data) { productData = data.products.filter(x => x.id == product.attr("product_id"))[0] if (productData) { newValue = productData.cnt_item counter.val(newValue); if (productData.error) { ShowWarningNotification(productData.error) } }; let newAmount = parseFloat(product.attr("amount_item")) * newValue let priceText = `${(formatNumber(newAmount))} ${getCurrencySymbol(typeCurrency)}` product.find('[name="item-price"]').text(priceText) if (newValue == 1) { product.find('[name="item-counter-minus"]').prop("disabled", true) } recalculateTotalPrice() recalculateTotalItems() hideItemLoader(ev) }) } function showDeleteModal(ev) { let product = $(ev.target).closest("[product_id]"); let template = document.getElementById("delete-item-modal"); let modal = template.content.cloneNode(true); $(modal).find('[action="delete"]').on("click", function () { deleteProductFromCart(ev); $("div[name='delete-item-modal']").remove(); RemoveUnderlay(); }) $(modal).find('[action="close"]').on("click", function () { $("div[name='delete-item-modal']").remove(); RemoveUnderlay(); }) AddUnderlay(); document.body.appendChild(modal); } function deleteProductFromCart(ev) { let product = $(ev.target).closest("[product_id]"); changeCount2(product.attr("cart_id_number"), 0, userLanguage) .then(function () { product.remove(); recalculateTotalPrice() recalculateTotalItems() }) } function itemsCountChanged(ev) { let product = $(ev.target).closest("[product_id]"); let counter = product.find('[name="item-counter"]'); let currentValue = 1 if (counter.val()) { currentValue = parseInt(counter.val()); } else { counter.val(currentValue) }; currentValue = Math.max(currentValue, 1); showItemLoader(ev) setCounterFieldStyle(counter, currentValue) changeCount2(product.attr("cart_id_number"), currentValue, userLanguage) .then(function (data) { productData = data.products.filter(x => x.id == product.attr("product_id"))[0] if (productData) { currentValue = productData.cnt_item counter.val(currentValue); if (productData.error) { ShowWarningNotification(productData.error) } }; let newAmount = parseFloat(product.attr("amount_item")) * currentValue let priceText = `${(formatNumber(newAmount))} ${getCurrencySymbol(typeCurrency)}` product.find('[name="item-price"]').text(priceText) recalculateTotalPrice() recalculateTotalItems() hideItemLoader(ev) }) } function ShowWarningNotification(message) { showNotif(message, "warning") } function recalculateTotalPrice() { let total = $("#cart-items").find('[product_id][amount_item]').toArray().reduce((partialSum, a) => partialSum + parseFloat($(a).attr("amount_item")) * parseInt($(a).find('[name="item-counter"]').val()), 0) let totalPriceText = `${(formatNumber(total))} ${getCurrencySymbol(typeCurrency)}` $('[name="total-price"]').text(totalPriceText) } function recalculateTotalItems() { let totalItems = $("#cart-items").find('[product_id][amount_item] input[name="item-counter"]').toArray().reduce((partialSum, a) => partialSum + parseInt($(a).val()), 0); if (totalItems == 0) { $('[name="cart-items-count"]').addClass("d-none"); $("#deleteAllButton").addClass("d-none"); showEmptyResult() $("#in-cart-counter").addClass("d-none"); $("#in-cart-counter").text(totalItems); $("#bottom_bar #cobtn2").attr("disabled", "disabled") } else { let itemsCount = GetItemsCountText(totalItems) $('[name="cart-items-count"]').text(itemsCount) $("#in-cart-counter").removeClass("d-none"); $("#in-cart-counter").text(totalItems); $("#bottom_bar #cobtn2").removeAttr("disabled") } } function deleteAllItemsFromCart(isAvailable = true) { let template = document.getElementById("delete-all-items-modal"); let modal = template.content.cloneNode(true); $(modal).find('[action="delete"]').on("click", function () { let products = isAvailable ? $('[product_id][amount_item]') : $('[product_id]:not(:amount_item)'); Promise.all(products.toArray().map(p => { changeCount2($(p).attr("cart_id_number"), 0, userLanguage) })).then(function () { products.remove(); recalculateTotalPrice() recalculateTotalItems() }) $("div[name='delete-all-items-modal']").remove(); RemoveUnderlay(); }) $(modal).find('[action="close"]').on("click", function () { $("div[name='delete-all-items-modal']").remove(); RemoveUnderlay(); }) AddUnderlay(); document.body.appendChild(modal); } function submitPurchaseForm() { let form = $('#purchase-form'); let cart_uid = $.cookie("cart_uid") form.find('[name="TypeCurr"]').val(userCurrency) form.find('[name="Lang"]').val(userLanguage) form.find('[name="Cart_UID"]').val(cart_uid) form.find('[name="FailPage"]').val(window.location.href) form.submit(); } function initPage() { loadCartProducts(userCurrency).then(function () { $("button[action='toggleInFavorites']").on("click", toggleInFavorites); $('[name="item-counter-plus"]:not([locked])').on("click", increaseProductCount); $('[name="item-counter-minus"]:not([locked])').on("click", decreaseProductCount); $('[name="item-delete"]').on("click", showDeleteModal); $('[name="item-counter"]:not([locked])').on("change", itemsCountChanged); }) } initPage() $('body') .on('click', '.id_cart_del_all', function (e) { e.preventDefault() deleteAllItemsFromCart(true) }) .on('click', '.id_cart_submit', function (e) { e.preventDefault() submitPurchaseForm() }) }); $(function () { var $btn = $("#cobtn1"); if ($btn.length < 1) { return; } var $bar = $("#bottom_bar"); var shown = false; $(window).on("scroll resize", $.throttle(100, function () { fixBar() })); fixBar(); let hideBarTimeout = null function fixBar() { if (isInView($btn)) { if (shown) { shown = false; $bar.removeClass("show"); hideBarTimeout = setTimeout(() => $bar.addClass("d-none"), 150) } } else { if (!shown) { shown = true; clearTimeout(hideBarTimeout) if (!$bar.find('button').attr("disabled")) { $bar.removeClass("d-none") $bar.addClass("show"); } } } } function isInView(el) { var rect = el[0].getBoundingClientRect(); return rect.bottom > 80 && rect.top < (window.innerHeight || document.documentElement.clientHeight) - 100; } });