document.addEventListener("DOMContentLoaded", () => {
fetchCart();
const myOffcanvas = document.getElementById('side-cart');
myOffcanvas.addEventListener('hidden.bs.offcanvas', () => {
$('.item-inside-cart').html('');
$('.loading-cart').removeClass('d-none');
$('.footer-side-cart').addClass('d-none');
});
myOffcanvas.addEventListener('show.bs.offcanvas', () => {
$('.loading-cart').addClass('d-none');
fetchCart();
});
});
function productCartAddToCart(elm, product_id) {
const $elm = $(elm);
$('.loading-cart').removeClass('d-none');
const originalHtml = $elm.html();
$elm.html(`
Loading...
`);
addToCart(product_id, 1, async () => {
$elm.html(originalHtml);
await fetchCart();
new bootstrap.Offcanvas(document.getElementById('side-cart')).show();
});
}
function addToCart(product_id, quantity, onCompleted) {
zid.store.cart.addProduct({ productId: product_id, quantity }).then(response => {
if (response.status === 'success') {
setCartTotalAndBadge(response.data.cart);
onCompleted?.();
}
});
}
function setCartTotalAndBadge(cart) {
setCartBadge(cart.products_count);
const cartTotal = getCartTotal(cart);
if (cartTotal) setCartIconTotal(cartTotal);
}
function setCartIconTotal(total) {
$('.cart-price').html(total).removeClass('d-none');
}
function setCartBadge(badge) {
$('.cart-badge').html(badge);
}
function displayActivePaymentSessionBar(cart) {
$('.payment-session-bar').toggleClass('d-none', !cart.is_reserved);
}
function getCartTotal(cart) {
const totalItem = cart?.totals?.find(total => total.code === 'total');
return totalItem?.value_string || null;
}
function createCartProduct(product) {
const oldPrice = product.price_before_string
? `${product.price_before_string}${product.price_string}`
: `${product.price_string}`;
const imageUrl = product.images[0].origin;
return `
`;
}
function createCartProductBundle(item) {
return [...item.product_x, ...item.product_y].map(createCartProduct).join('');
}
function fetchCart() {
$('.loading-cart').removeClass('d-none');
$('#additional-cart').html('');
$('.side-cart-items').removeClass('d-none');
const emptyText = $('#side-cart').data('empty');
zid.store.cart.fetch().then(response => {
if (response.status !== "success") return alert('Error Cart');
const cart = response.data.cart;
let itemsHtml = '';
let totalsHtml = '';
$('.cart-count').html(cart.products_count);
if (cart.products_count > 0) {
cart.products.forEach(product => {
itemsHtml += product.bundle_name ? createCartProductBundle(product) : createCartProduct(product);
});
cart.totals.forEach(total => {
totalsHtml += `
${total.title}
${total.value_string}
`;
});
$('#cart-side-totals').html(totalsHtml);
$('.side-cart-items').html(itemsHtml);
$('.footer-side-cart').removeClass('d-none');
} else {
$('#additional-cart').html(`
${emptyText}
`);
$('#cart-side-totals, .side-cart-items').html('');
$('.footer-side-cart').addClass('d-none');
}
setCartTotalAndBadge(cart);
displayActivePaymentSessionBar(cart);
}).finally(() => {
$('.loading-cart').addClass('d-none');
});
}
function removeItem(key, item) {
$(`.side-cart-items li#product_${key} .remove`).html('');
zid.store.cart.removeProduct(key).then(response => {
if (response.status === 'success') fetchCart();
});
}
$(document).on('click', '.btn-number', function () {
const type = $(this).attr('data-type');
const fieldName = $(this).attr('data-field');
const input = $(`input[name='${fieldName}']`);
let currentVal = parseInt(input.val());
if (!isNaN(currentVal)) {
if (type === 'plus' && currentVal < parseInt(input.attr('max'))) {
input.val(currentVal + 1).change();
} else if (type === 'minus' && currentVal > parseInt(input.attr('min'))) {
input.val(currentVal - 1).change();
}
}
});
function updateMiniCartProduct(productId, quantity) {
const cartItems = $('.side-cart-items');
cartItems.fadeTo('slow', 0.3);
zid.store.cart.updateProduct(productId, quantity).then(response => {
cartItems.fadeTo('slow', 1);
if (response.status === 'success') {
fetchCart();
} else {
$.toast({
text: response.data.message,
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 2000,
position: 'top-right',
textAlign: 'center',
bgColor: '#d90000',
textColor: '#fff',
});
}
});
}