【问题标题】:simpleCart.js Update Cart Item if existingsimpleCart.js 更新购物车项目(如果存在)
【发布时间】:2014-07-14 19:05:34
【问题描述】:

使用 simpleCartjs 购物车框架,当商品添加到购物车

时,我希望执行以下过程
  1. 检查商品名称是否已在购物车商品列表中。
  2. 从购物车项目列表中删除所述项目。
  3. 使用新的购物车商品属性添加点击的商品。

以下代码阻止添加相同数量的相同商品,但是如果我增加商品数量(我有一个更改 item_Quantity 范围的下拉菜单),它将向购物车添加一个新商品。而且价格会根据数量而变化。我有自己的 js 代码来更新 item_price 跨度。

simpleCart.bind('beforeAdd', function (item) {

    if (simpleCart.has(item)) {
        item.remove();
        return false;
    }
});

第 1 项:名称 =“2014/06/06”数量 =“2”

第 2 项:名称 = "2014/06/07" 数量 = "1"

如果我尝试使用 Quantity = "3" 添加项目 1,它会添加一个新项目,结果是:

第 1 项:名称 =“2014/06/06”数量 =“2”

第 2 项:名称 = "2014/06/07" 数量 = "1"

第 3 项:名称 = “2013/06/06”数量 = “3”

如果可能,我需要减轻这种行为。因为我需要删除该项目并重新添加所有 attr,因为我的服务器端验证将根据添加的数量响应新的 attr。

【问题讨论】:

    标签: javascript html simplecart


    【解决方案1】:

    This article helped amazingly and gave me the answer

    simpleCart.bind('beforeAdd', function (newitem) {
           simpleCart.each(function (cartitem) {
            if (cartitem.get("name") === newitem.get("name")) {
                cartitem.remove();
            }
        });
    
    });
    

    【讨论】:

    • 这段代码非常有用!我一直在到处寻找这个。谢谢大佬!
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2020-05-09
    • 2023-04-08
    • 2014-08-18
    相关资源
    最近更新 更多