简体   繁体   English

购物车总价格和数量

[英]Shopping Cart Total Price and Quantity

Below there's a shopping cart that when you remove the product it works and price gets updated, but when I try to add more quantity like 2 or 3 then Total Price doesn't get updated, but when I remove the product it gets updated and that's where's the problem that I don't know how to solve it. 下面有一个购物车,当你删除产品时,它工作,价格得到更新,但当我尝试添加更多的数量,如2或3,然后总价格不会更新,但当我删除产品,它得到更新,这是哪里是我不知道如何解决它的问题。 Also I have added an function with quantity so the minimum of order could be only 1 and if you try to add less than 1 like 0 or -1,-2 it will be changed automatically at number 1, but doesn't work and I need some help. 此外,我添加了一个数量函数,所以最小的顺序可能只有1,如果你尝试添加少于1,如0或-1,-2它将自动更改为1号,但不起作用,我需要一些帮助。

ps: This is only an exercise so there's no PHP or something else. ps:这只是一个练习,所以没有PHP或其他东西。

<div class="click-cart">
        <div class="cart-row">
            <span class="cart-item cart-header cart-column">ITEM</span>
            <span class="cart-price cart-header cart-column">PRICE</span>
            <span class="cart-quantity cart-header cart-column">QUANTITY</span></div>
          <div class="olp">
            <div class="lakn">
            <img class="shop-item-cart" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg">
            <span class="shop-title">Product 1</span>
            <span class="shop-price">$19.99</span>
            <input class="quantity-input" type="number" value="1">
            <button class="delete-cart">X</button>
          </div></div>
            <div class="clear-checkout">
          <button class="checkout">Checkout</button>
          <button class="clear-cart">Clear Cart</button></div>
          <div class="cart-total">
              <strong class="cart-total-title">Total</strong>
              <span class="cart-total-price">$10.79</span>
          </div>
      </div>```
-------------------------------------------------------------------------
let removeCartItemButtons = document.querySelectorAll('.delete-cart'); 
    for (let i = 0; i < removeCartItemButtons.length; i++) {
    let button = removeCartItemButtons[i]
    button.addEventListener('click', removeCartItem) 
    }

    let quantityInputs = document.querySelector('.quantity-input');
    for (let i = 0; i < quantityInputs.length; i++) {
        let input = quantityInputs[i]
        input.addEventListener('change', quantityChanged);
    }

function removeCartItem (event) {
        let buttonCliked = event.target;
        buttonCliked.parentElement.parentElement.remove()
        updateCartTotal()
    }

    function quantityChanged () {
        let input = event.target
        if (isNaN(input.value) || input.value <= 0) {
        input.value = 1
    }
    updateCartTotal ()
}

function updateCartTotal () {
    let cartItemContainer = document.querySelector('.click-cart');
    let cartRows = cartItemContainer.querySelector('.cart-row');
    let total = 0
    for (let i = 0; i < cartRows.length; i++) {
        let cartRow = cartRows[i]
        let priceElement = cartRow.querySelector('.shop-price');
        let quantityElement = cartRow.querySelectorAll('.quantity-input');
        let price = parseFloat(priceElement.innerText.replace('$', ''))
        let quantity = quantityElement.value
        total = total + (price * quantity)
    }
    total = Math.round(total * 100) / 100
    document.querySelector('.cart-total-price').innerText = '$' + total;
}
    <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" >
        <div>
            <div class="click-cart">
                <div class="cart-row2">
                    <span class="cart-item cart-header cart-column">ITEM</span>
                    <span class="cart-price cart-header cart-column">PRICE</span>
                    <span class="cart-quantity cart-header cart-column">QUANTITY</span>
                </div>
                <div class="cart-row">
                    <div class="lakn">
                        <img class="shop-item-cart" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg">
                        <span class="shop-title">Product 1</span>
                        <span class="shop-price">$19.99</span>
                        <input class="quantity-input" type="number" value="1" />
                        <button class="delete-cart">X</button>
                    </div>
                </div>
                <div class="clear-checkout">
                    <button class="checkout">Checkout</button>
                    <button class="clear-cart">Clear Cart</button>
                </div>
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price">$10.79</span>
                </div>
            </div>
        </div>
    </form>
    <script>
        let removeCartItemButtons = document.querySelectorAll('.delete-cart');
        for (let i = 0; i < removeCartItemButtons.length; i++) {

            let button = removeCartItemButtons[i]
            button.addEventListener('click', removeCartItem)
        }

        let quantityInputs = document.querySelectorAll('.quantity-input');
        for (let i = 0; i < quantityInputs.length; i++) {

            let input = quantityInputs[i]
            input.addEventListener('change', quantityChanged);
        }

        function removeCartItem(event) {
            let buttonCliked = event.target;
            buttonCliked.parentElement.parentElement.remove()
            updateCartTotal()
        }

        function quantityChanged() {

            let input = event.target;
            if (isNaN(input.value) || input.value <= 0) {
                input.value = 1;
            }
            updateCartTotal()
        }

        function updateCartTotal() {
            let cartItemContainer = document.querySelector('.click-cart');
            let cartRows = cartItemContainer.querySelectorAll('.cart-row');

            let total = 0
            for (let i = 0; i < cartRows.length; i++) {
                let cartRow = cartRows[i]
                let priceElement = cartRow.querySelector('.shop-price');
                let quantityElement = cartRow.querySelector('.quantity-input');

                let price = parseFloat(priceElement.innerText.replace('$', ''))
                let quantity = quantityElement.value
                total = total + (price * quantity)
            }
            total = Math.round(total * 100) / 100
            document.querySelector('.cart-total-price').innerText = '$' + total;
        }
    </script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数量,单价和总成本购物车 - Find the Quantity, Unit Price and Total Cost Shopping Cart 有没有一种合并凭证值的方法(购物车的总价格和数量) - Is there a way to merge document values (shopping cart total price and quantity) 计算购物车发票的总价 - computing total price of shopping cart invoice 带有 Javascript 香草的购物车。 // 键值问题(数量和价格) - Shopping cart with Javascript vanilla. // key value problem (quantity and price) 淘汰赛:无法将数量变化与购物车中的总成本绑定 - Knockout: Unable to bind change in quantity to total cost in shopping cart 加入购物车前如何计算产品总价? - How to calculate product's total price before adding it to the shopping cart? 在 React/Redux 中,如何计算购物车的总价 - In React/Redux, how to calculate a Total price for a shopping cart 购物车删除列表同时更新总价 - Shopping Cart Delete List To Also Update Total Price 反应本地添加到购物车数量和总价问题 - React native add to cart quantity and total price issue 计算购物车中数量增加或减少时每行的总价。 (数量*价格) - calculate each row total price as the quantity increase or decrese in shoping cart. (quantity * price)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM