简体   繁体   English

获取购物车中物品的价值并相应地移除库存

[英]Get value of items in shopping cart and remove stock accordingly

So I'm trying to make a stock type of system where when a user clicks a button it checks which Items are in the shopping cart and what their names are and the subtotal price of the single product.所以我正在尝试创建一个库存类型的系统,当用户单击一个按钮时,它会检查购物车中的哪些项目以及它们的名称以及单个产品的小计价格。

So if the subtotal is 1$ I know that the quantity of that product is 1 and then subtract that from the stock in the MySQL database, and if the subtotal is 2$ I know that the quantity is 2 and subtract 2 of the stock of that specific product.因此,如果小计是1 美元,我知道该产品的数量是1 ,然后从 MySQL 数据库中的库存中减去它,如果小计是2 美元,我知道数量是2 ,然后减去2的库存那个特定的产品。

What I've done so far is get the first h4 text and check if that is equal to a product name and then check the price to get the quantity and all of that would be in a if else statement which is a pretty bad way of doing it since I would have to make about 360 if else checks.到目前为止,我所做的是获取第一个h4文本并检查它是否等于产品名称,然后检查价格以获取数量,所有这些都将在 if else 语句中,这是一种非常糟糕的方式这样做是因为如果其他检查,我将不得不制作大约360 度

This is an example of how a shopping cart could look like and there can be more products in the shopping cart or the price could be more since the quantity per product can be higher这是一个购物车外观的示例,购物车中可能有更多产品,或者价格可能更高,因为每个产品的数量可能更高

<div class="list-group sc-cart-item-list">
    <div class="sc-cart-item list-group-item" data-unique-key="1586696968143">
        <button type="button" class="sc-cart-remove">×</button>
        <img class="img-responsive pull-left" src="">
        <h4 class="list-group-item-heading">Iphone 11 6.1</h4>
        <p class="list-group-item-text">Yellow</p>
        <div class="sc-cart-item-summary">
            <span class="sc-cart-item-price">19,99&nbsp;€</span> × <input type="number" min="1" max="1000" class="sc-cart-item-qty" value="1"> = <span class="sc-cart-item-amount">19,99&nbsp;€</span>
    </div>
</div>

<div class="sc-cart-item list-group-item" data-unique-key="1586697501762">
    <button type="button" class="sc-cart-remove">×</button>
    <img class="img-responsive pull-left" src="">
    <h4 class="list-group-item-heading">Iphone XS Max</h4>
    <p class="list-group-item-text">Orange</p>
    <div class="sc-cart-item-summary">
        <span class="sc-cart-item-price">19,99&nbsp;€</span> × <input type="number" min="1" max="1000" class="sc-cart-item-qty" value="1"> = <span class="sc-cart-item-amount">19,99&nbsp;€</span>
    </div>
</div>
</div>

This is the script I have so far but its generally very bad since first of all the part where I check the price in a if statement doesn't work and I would have to do this 360 times over for every possible quantiy someone can buy and every possible variation of the product这是我到目前为止的脚本,但它通常非常糟糕,因为首先我在 if 语句中检查价格的部分不起作用,我必须为每个可能的数量做 360 次有人可以购买和产品的所有可能变化

<script type="text/javascript">

$(document).ready(function() {
    $("#vbutton").click(function() {
        alert("clicked");
        var w = $('h4', '').first().text();
        //console.log(v);

        var a = $('.sc-cart-item-amount', '').first().text();
        //console.log(a);



        if (w !== '') {

            console.log("Hi");

            if (w == "Iphone 11 6.1") {

                console.log(a);
                if (a == '19,99 €') {
                    console.log("removed 1 from database");
                }

            }


        }

    });
});

</script>

Then I would need to do this for the second, third, fourth and so on text that could be in the shopping Cart.然后我需要对可能在购物车中的第二个、第三个、第四个等文本执行此操作。 I would really appreciate any help possible, thank you.我真的很感激任何可能的帮助,谢谢。

Do you want to say you want to parse the ''19,99 €''?你想说你要解析''19,99 €''吗?

parseInt('19,99 €'.replace(/,/g, ''));
// 1999

Then you can do some calculate, and get the quantity.然后你可以做一些计算,并得到数量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM