This article describes the client shopping cart operations implemented by Jquery.cookie. Share to everyone for your reference, specific as follows:
Shopping cart var cart = function () {this.
Count = 0; This.
Total = 0; This.
Items = new Array ();
}; Shopping Cart Collection Object var cartitem = function () {this.
Id = 0; This.
Name = ""; This.
Count = 0; This.
Price = 0;
};
Shopping cart operation var carthelper = function () {this.cookiename = "yxhcart"; This.
Clear = function () {var cart = new cart (); This.
Save (CART);
return cart;
}; Add this to your shopping cart. ADD = function (ID, name, count, price) {var cart = this.
Read (); var index = this.
Find (ID); If ID already exists, overwrite quantity if (Index >-1) {cart. Total-= ((cart. Items[index]. Count *) * (cart. Items[index].
Price * 100))/10000); Cart. Items[index].
Count = Count; Cart. Total + = ((cart. Items[index]. Count *) * (cart. Items[index].
Price * 100))/10000);
else {var item = new Cartitem (); Item.
id = ID; Item.
name = name; Item.
Count = Count; Item.
Price = Price; Cart.
Items.push (item); Cart.
count++; Cart. Total + = ((cart.Items[index]. Count *) * (cart. Items[index].
Price * 100))/10000); } this.
Save (CART);
return cart;
}; Change quantity this. Change = function (ID, count) {var cart = this.
Read (); var index = this.
Find (ID); Cart. Items[index].
Count = Count; This.
Save (CART);
return cart;
}; Move out of the shopping cart this. Del = function (id) {var cart = this.
Read (); var index = this.
Find (ID); if (Index >-1) {var item = cart.
Items[index]; Cart.
count--; Cart. Total = cart. Total-((item. Count *) * (item.
Price * 100))/10000); Cart.
Items.splice (index, 1); This.
Save (CART);
return cart;
}; Find this by ID. Find = function (id) {var cart = this.
Read ();
var index =-1; for (var i = 0; i < cart. Items.length; i++) {if (cart). Items[i].
ID = = id) {index = i;
} return index;
}; Cookie operation this.
Save = function (cart) {var source = ""; for (var i = 0; i < cart. Items.lengtH
i++) {if (source!= "") {source + = "|$|";} Source = this. Itemtostring (cart.
Items[i]);
} $.cookie (This.cookiename, source);
}; This.
Read = function () {//reading the collection in the cookie var Source = $.cookie (this.cookiename);
var cart = new cart ();
if (Source = null | | source = = "") {return cart;
var arr = source.split ("|$|"); Cart.
Count = Arr.length; for (var i = 0; i < arr.length; i++) {var item = this.
Itemtoobject (Arr[i]); Cart.
Items.push (item); Cart. Total + = ((item. Count *) * (item.
Price * 100))/10000);
return cart;
}; This. itemtostring = function (item) {return item. Id + "| |" + ESCAPE (item. Name) + "| |" + Item. Count + "| |" + Item.
Price;
}; This.
Itemtoobject = function (str) {var arr = str.split (' | | | ');
var item = new Cartitem (); Item.
Id = arr[0]; Item.
Name = unescape (arr[1]); Item.
Count = arr[2]; Item.
Price = Arr[3];
return item;
};
};
I hope this article will help you with your jquery programming.