expressCart/public/javascripts/expressCart.min.js

1 line
14 KiB
JavaScript
Raw Normal View History

2020-02-01 16:59:30 +10:00
function checkMaxQuantity(t,a){if($("#maxQuantity").length){if(46===t.keyCode||8===t.keyCode)return;if(parseInt($(t.target).val())>parseInt($("#maxQuantity").val())){const e=a.val();t.preventDefault(),a.val(e.slice(0,-1)),showNotification(`Exceeds maximum quantity: ${$("#maxQuantity").val()}`,"warning",!1)}}}function deleteFromCart(t){$.ajax({method:"POST",url:"/product/removefromcart",data:{cartId:t.attr("data-cartid")}}).done(function(t){showNotification(t.message,"success"),updateCartDiv()}).fail(function(t){showNotification(t.responseJSON.message,"danger")})}function cartUpdate(t){$(t).val()>0?""!==$(t).val()&&updateCart(t):$(t).val(1)}function updateCart(t){$.ajax({method:"POST",url:"/product/updatecart",data:{cartId:t.attr("data-cartid"),productId:t.attr("data-id"),quantity:t.val()}}).done(function(t){updateCartDiv()}).fail(function(t){showNotification(t.responseJSON.message,"danger",!0)})}function getSelectedOptions(){var t={};return $(".product-opt").each(function(){var a=$(this).val().trim(),e=$(this).attr("data-label"),n=$(this).attr("name"),o=$(this).attr("type");o||(t[n]={label:e,name:n,value:a}),"radio"===o&&(t[n]={label:e,name:n,value:$('input[name="'+n+'"]:checked').val()}),"checkbox"===o&&(t[n]={label:e,name:n,value:$('input[name="'+$(this).attr("name")+'"]').is(":checked")})}),t}function updateCartDiv(){$.ajax({method:"GET",url:"/checkout/cartdata"}).done(function(t){var a=t.cart,e=t.session,n="",o=numeral(e.totalCartAmount).format("0.00"),i=numeral(e.totalCartShipping).format("0.00"),r=`${e.shippingMessage} :<strong id="shipping-amount">${t.currencySymbol}${i}</strong>`;0===e.totalCartShipping&&(r=`<span id="shipping-amount">${e.shippingMessage}</span>`);var c=numeral(e.totalCartDiscount).format("0.00"),s="";e.totalCartDiscount>0&&(s=`\n <div class="text-right">\n Discount: <strong id="discount-amount">${t.currencySymbol}${c}</strong>\n </div>`),a?($("#cart-empty").empty(),Object.keys(a).forEach(function(e){var o=a[e],i=numeral(o.totalItemPrice).format("0.00"),r="",c=1;Object.keys(o.options).forEach(function(t){var a=o.options[t];c===Object.keys(o.options).length?r+=`<strong>${upperFirst(a.name)}</strong>: ${a.value}`:r+=`<strong>${upperFirst(a.name)}</strong>: ${a.value} / `,c++});var s=`<img class="img-fluid" src="/uploads/placeholder.png" alt="${o.title} product image"></img>`;o.productImage&&(s=`<img class="img-fluid" src="${o.productImage}" alt="${o.title} product image"></img>`),n+=`\n <div class="d-flex flex-row bottom-pad-15">\n <div class="p-2 cart-product">\n <div class="row h-200">\n <div class="col-4 col-md-3 no-pad-left">\n ${s}\n </div>\n <div class="col-8 col-md-9">\n <div class="row">\n <div class="col-12 no-pad-left mt-md-4">\n <h6><a href="/product/${o.link}">${o.title}</a></h6>\n ${r}\n </div>\n <div class="col-12 col-md-6 no-pad-left mb-2">\n <div class="input-group">\n <div class="input-group-prepend">\n <button class="btn btn-primary btn-qty-minus" type="button">-</button>\n </div>\n <input type="number" class="form-control cart-product-quantity text-center" data-cartid="${e}" data-id="${o.id}" maxlength="2" value="${o.quantity}">\n <div class="input-group-append">\n <button class="btn btn-primary btn-qty-add" type="button">+</button>\n </div>\n </div>\n