-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopping_cart.js
116 lines (105 loc) · 3.93 KB
/
shopping_cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function shopping_cart(owner) {
this.owner = $.trim(owner);
this.skuArray = new Array();
this.qtyArray = new Array();
this.priceArray = new Array();
//////////////////////////////////////////////////////////////////////////
// Do not use the following two methods; they are private to this class
this.getCookieValues = function() { // PRIVATE METHOD
var raw_string = document.cookie;
var arr = new Array();
if(raw_string == undefined)
return;
var tmp = raw_string.split(";");
var myValue = null;
for(i=0; i < tmp.length; i++)
if(tmp[i].indexOf(owner) != -1)
myValue = tmp[i].split("=");
if(!myValue)
return;
arr = myValue[1].split("||");
for(i=0; i < arr.length; i++) {
var pair = arr[i].split("|");
if(pair[0] == undefined || pair[1] == undefined || pair[2] == undefined) continue;
this.skuArray[i] = pair[0];
this.qtyArray[i] = pair[1];
this.priceArray[i] = pair[2];
}
}
this.writeCookie = function() { // PRIVATE METHOD
var toWrite = this.owner+"=";
for(i=0; i < this.skuArray.length; i++)
toWrite += this.skuArray[i] + "|" + this.qtyArray[i] + "|" + this.priceArray[i] + "||";
toWrite = toWrite.substring(0,toWrite.length - 2);
toWrite += "; path=/";
document.cookie = toWrite;
}
//////////////////////////////////////////////////////////////////////////
this.add = function(sku, quantity) {
sku = $.trim(sku);
var res = sku.split("%");
quantity = $.trim(quantity);
this.getCookieValues();
var found = false;
for(i=0; i < this.skuArray.length; i++)
if(this.skuArray[i] == res[0]) {
this.qtyArray[i] = parseInt(quantity,10) + parseInt(this.qtyArray[i],10);
found = true;
}
if(!found) {
this.skuArray.push(res[0]);
this.qtyArray.push(quantity);
this.priceArray.push(res[1]);
}
this.writeCookie();
}
this.setQuantity = function(sku, quantity) {
sku = $.trim(sku);
var found = false;
if(sku == "") return;
quantity = $.trim(quantity);
this.getCookieValues();
for(i=0; i < this.skuArray.length; i++)
if(this.skuArray[i] == sku) {
this.qtyArray[i] = parseInt(quantity,10);
found = true;
}
if(found)
this.writeCookie();
}
this.delete = function(sku) {
sku = $.trim(sku);
var index = -1;
this.getCookieValues();
for(i=0; i < this.skuArray.length; i++)
if(this.skuArray[i] == sku)
index = i;
if(index != -1) {
this.skuArray.splice(index,1);
this.qtyArray.splice(index,1);
}
if(this.skuArray.length == 0) {
document.cookie = this.owner + "= ;expires=-1;path=/";
}
else
this.writeCookie();
}
this.size = function() {
this.getCookieValues();
var count = 0;
for(i=0; i < this.qtyArray.length; i++)
count += parseInt(this.qtyArray[i],10);
return count;
}
this.getCartArray = function() {
this.getCookieValues();
var returnArray = new Array();
for(i=0; i < this.skuArray.length; i++) {
returnArray[i] = new Array();
returnArray[i].push(this.skuArray[i]);
returnArray[i].push(this.qtyArray[i]);
returnArray[i].push(this.priceArray[i]);
}
return returnArray;
}
}