document.addEventListener("DOMContentLoaded", function(){ fetchCart(); }); function productCartAddToCart(elm, product_id) { var html = $(elm).html(); $(elm).html('
Loading...
'); addToCart(product_id, 1 , function () { $(elm).html(html); $.toast({ text: $('body').attr('data-cartsuccess'), showHideTransition: 'fade', allowToastClose: true, hideAfter: 2000, position: 'top-right', textAlign: 'center', bgColor: '#d4edda', textColor: '#155724', }); if($("#gift-tooltip").length > 0){ $("#gift-tooltip").removeClass('d-none'); setTimeout(()=>{ $("#gift-tooltip").addClass('d-none'); },7000) } }) } function addToCart(product_id, quantity, onCompleted) { zid.store.cart.addProduct({productId: product_id, quantity: quantity}).then(function (response) { if(response.status === 'success'){ setCartTotalAndBadge(response.data.cart); if(onCompleted){ onCompleted(); } } }) } function setCartTotalAndBadge(cart) { setCartBadge(cart.products_count) var cartTotal = getCartTotal(cart); if(cartTotal){ setCartIconTotal(cartTotal) } } function setCartIconTotal(total) { $('.cart-header-total').html(total) } function setCartBadge(badge) { if(badge > 0){ $('.cart-badge').removeClass('d-none'); $('.cart-badge').html(badge); }else { $('.cart-badge').addClass('d-none'); } } function displayActivePaymentSessionBar(cart) { if(cart.is_reserved){ $('.payment-session-bar').removeClass('d-none') } } function fetchCart() { zid.store.cart.fetch().then(function (response) { if(response.status === 'success'){ if(response.data) { setCartItem(response.data.cart); setCartTotalAndBadge(response.data.cart); setCartTotal(response.data.cart?.total?.value_string); displayActivePaymentSessionBar(response.data.cart); } } }) } function getCartTotal(cart) { if(cart && cart.totals && cart.totals.length > 0){ var cartTotalItem = cart.totals.filter(function (total) { return (total.code === 'total') }) if(cartTotalItem.length > 0){ return cartTotalItem[0].value_string; } } return null; } function setCartItem(items){ var html_item = ''; if(items.products.length){ items.products.map(function (product){ html_item = `
`; html_item += `
`; var new_price = product.price_string var price =''; if (product.price_before_string) { var old_price = product.price_before_string price = `
${old_price}
${new_price}
`; } else { price = `
${new_price}
`; } html_item += `
${product.name}
${price}
${product.quantity}x
`; html_item += `
`; }); //not-items $('#list-header-cart').html(html_item); $('.dropdown-cart').removeClass('not-items'); }else{ //not-items $('#list-header-cart').html(''); $('.dropdown-cart').addClass('not-items'); } } function setCartTotal(total){ if(typeof total !== 'undefined' && total){ $('.total-price').html(total).removeClass('d-none'); }else{ $('.total-price').html(total).addClass('d-none'); } } function removeItem(key,item){ $('li#product_'+key+' .remove').html('
'); zid.store.cart.removeProduct(key).then(function (response) { if(response.status ==='success'){ fetchCart(); } }); }