简体   繁体   English

simpleCart.js更新购物车项目(如果存在)

[英]simpleCart.js Update Cart Item if existing

Using the simpleCartjs shopping cart framework I am looking to do the following procedure when an item is Added to Cart 使用simpleCartjs购物车框架将物品添加到购物车时,我希望执行以下过程

  1. check if the item name is already in the cart items list. 检查商品名称是否已经在购物车商品列表中。
  2. Remove the said item from the cart items list. 从购物车项目列表中删除该项目。
  3. Add the clicked item with new cart item attributes. 添加具有新购物车商品属性的点击商品。

The following code, stops the same item of the same quantity from being added, however if i increase the item quantity (I have a dropdown that changes the item_Quantity span) It will add a new item to the cart. 下面的代码阻止添加相同数量的相同项目,但是,如果我增加了项目数量(我的下拉列表更改了item_Quantity跨度),它将向购物车添加一个新项目。 And the prices change based on quantity. 价格根据数量而变化。 I have my own js code that updates the item_price span. 我有自己的js代码,用于更新item_price跨度。

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

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

Item 1: Name = "2014/06/06" Quantity = "2" 项目1:名称=“ 2014/06/06”数量=“ 2”

Item 2: Name = "2014/06/07" Quantity = "1" 项目2:名称=“ 2014/06/07”数量=“ 1”

If I try to add Item 1 with Quantity = "3" it adds a new item resulting in: 如果我尝试添加数量=“ 3”的项目1,则会添加一个导致以下结果的新项目:

Item 1: Name = "2014/06/06" Quantity = "2" 项目1:名称=“ 2014/06/06”数量=“ 2”

Item 2: Name = "2014/06/07" Quantity = "1" 项目2:名称=“ 2014/06/07”数量=“ 1”

Item 3: Name = "2013/06/06" Quantity = "3" 项目3:名称=“ 2013/06/06”数量=“ 3”

I need to alleviate this behavior if possible. 如果可能,我需要减轻这种行为。 As I need to remove the item and re-add all the attr as my server side validation will respond to the new attr based on the quantity added. 由于我需要删除项目并重新添加所有属性,因此服务器端验证将根据添加的数量对新属性做出响应。

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();
        }
    });

});

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

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