I am developing the cart page for a demo by HTML, CSS and Javascript. In the cart, all…
- User
I am developing the cart page for a demo by HTML, CSS and Javascript. In the cart, all items in the cart will be separated by store, the item which have the same store will be grouped together. User can edit the quantity of the item and remove the whole item from cart, if the removed item is the last item of store, the ui of store item group will be removed too.
I found the bug that the store item group will removed when the user try to remove the item of store but not this is not the last item. Please review the following related code and fix it:
Javascript: function removeItem(removeProduct) {
/* Remove row from DOM and recalc cart total */ //if($("#cart_item_table_pc:visible").length > 0){ var productRow = $(removeProduct); const storeGroup = productRow.parent().closest('.store-group'); productRow.slideUp(fadeTime, function () { productRow.remove(); recalculateCart(); usercartTotal(); }); if (storeGroup.find('.cart-items').children('.cart-item').length === 1) { storeGroup.slideUp(fadeTime, function () { storeGroup.remove(); recalculateCart(); usercartTotal(); }) } //} if($("#cart_item_table:visible").length > 0){ var productRow_class = $(removeProduct).attr("class"); console.log(productRow_class); $("." + productRow_class).each(function() { $(this).slideUp(fadeTime, function () { $(this).remove(); recalculateCart(); usercartTotal(); }); }); } }function recalculateCart() {
var subtotal = 0; /* Sum up row totals */ $('.product').each(function () { subtotal += parseFloat($(this).children('.product-line-price').text()); }); /* Calculate totals */ var tax = subtotal * taxRate; var shipping = (subtotal > 0 ? shippingRate : 0); var total = subtotal + tax + shipping; /* Update totals display */ $('.totals-value').fadeOut(fadeTime, function () { $('#cart-subtotal').html(subtotal.toFixed(2)); $('#cart-tax').html(tax.toFixed(2)); $('#cart-shipping').html(shipping.toFixed(2)); $('#cart-total').html(total.toFixed(2)); if (total == 0) { $('.checkout').fadeOut(fadeTime); } else { $('.checkout').fadeIn(fadeTime); } $('.totals-value').fadeIn(fadeTime); }); }function usercartTotal() {
var cartTotal = 0; let HKFormatDollar = new Intl.NumberFormat('zh-Hant-HK', { style: 'currency', currency: 'HKD', }); $('#cart-container .store-group').each(function() { var storeName = $(this).data('store'); var totalSum = 0; $(this).find('.product-line-price').each(function() { var val = parseFloat($(this).data('value').toString().replace("HK$", ""))|| 0; totalSum += val; cartTotal += val; }); console.log('Store: ' + storeName + ' 總和為: ' + totalSum); }); $("#product_total").text(HKFormatDollar.format(cartTotal)); $("#final_total").text(HKFormatDollar.format(parseFloat(cartTotal) + parseFloat($("#shipping-fee").data('value')))); /* if($("#cart_table:visible").length > 0){ $("#cart_table > tbody > tr > .total-price > .product-line-price").each(function (i) { cartTotal = parseFloat(cartTotal) + parseFloat($(this).text().replace(/[^\d\.]/g, '')); }); $("#final_total").text(cartTotal.toFixed(2)); } */ - Assistant
- User
- Assistant
- User
The removeItem will be call by following code: $(document).on('click', '.product-removal button,.product-removal i,.product-sm-removal button', function (e) {
- Assistant
- User
- Assistant
- User
請提供完整的CSS設定
- Assistant
- User
- Assistant
- User
Please follow the template https://pic3.zhimg.com/v2-2b587cf5ddcd3c4ee0f62d7d7d346808_1440w.jpg to make the cart container layout by HTML, CSS
- Assistant