JS1986
JS1986

Reputation: 1950

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

  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. And the prices change based on quantity. I have my own js code that updates the item_price span.

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

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

Item 1: Name = "2014/06/06" Quantity = "2"

Item 2: Name = "2014/06/07" Quantity = "1"

If I try to add Item 1 with Quantity = "3" it adds a new item resulting in:

Item 1: Name = "2014/06/06" Quantity = "2"

Item 2: Name = "2014/06/07" Quantity = "1"

Item 3: Name = "2013/06/06" Quantity = "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.

Upvotes: 1

Views: 1630

Answers (1)

JS1986
JS1986

Reputation: 1950

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

});

Upvotes: 1

Related Questions