-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavascript for shopping cart.txt
113 lines (108 loc) · 3.45 KB
/
Javascript for shopping cart.txt
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
updateCartTotal();
document.getElementById("emptycart").addEventListener("click", emptyCart);
var btns = document.getElementsByClassName('addtocart');
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function() {addToCart(this);});
}
function addToCart(elem) {
var sibs = [];
var getprice;
var getproductName;
var cart = [];
var stringCart;
while(elem = elem.previousSibling) {
if (elem.nodeType === 3) continue;
if(elem.className == "price"){
getprice = elem.innerText;
}
if (elem.className == "productname") {
getproductName = elem.innerText;
}
sibs.push(elem);
}
//create product object
var product = {
productname : getproductName,
price : getprice
};
//convert product data to JSON for storage
var stringProduct = JSON.stringify(product);
/*send product data to session storage */
if(!sessionStorage.getItem('cart')){
//append product JSON object to cart array
cart.push(stringProduct);
//cart to JSON
stringCart = JSON.stringify(cart);
//create session storage cart item
sessionStorage.setItem('cart', stringCart);
addedToCart(getproductName);
updateCartTotal();
}
else {
//get existing cart data from storage and convert back into array
cart = JSON.parse(sessionStorage.getItem('cart'));
//append new product JSON object
cart.push(stringProduct);
//cart back to JSON
stringCart = JSON.stringify(cart);
//overwrite cart data in sessionstorage
sessionStorage.setItem('cart', stringCart);
addedToCart(getproductName);
updateCartTotal();
}
}
/* Calculate Cart Total */
function updateCartTotal(){
//init
var total = 0;
var price = 0;
var items = 0;
var productname = "";
var carttable = "";
if(sessionStorage.getItem('cart')) {
//get cart data & parse to array
var cart = JSON.parse(sessionStorage.getItem('cart'));
//get no of items in cart
items = cart.length;
//loop over cart array
for (var i = 0; i < items; i++){
//convert each JSON product in array back into object
var x = JSON.parse(cart[i]);
//get property value of price
price = parseFloat(x.price.split('$')[1]);
productname = x.productname;
//add price to total
carttable += "<tr><td>" + productname + "</td><td>$" + price.toFixed(2) + "</td></tr>";
total += price;
}
}
//update total on website HTML
document.getElementById("total").innerHTML = total.toFixed(2);
//insert saved products to cart table
document.getElementById("carttable").innerHTML = carttable;
//update items in cart on website HTML
document.getElementById("itemsquantity").innerHTML = items;
}
//user feedback on successful add
function addedToCart(pname) {
var message = pname + " was added to the cart";
var alerts = document.getElementById("alerts");
alerts.innerHTML = message;
if(!alerts.classList.contains("message")){
alerts.classList.add("message");
}
}
/* User Manually empty cart */
function emptyCart() {
//remove cart session storage object & refresh cart totals
if(sessionStorage.getItem('cart')){
sessionStorage.removeItem('cart');
updateCartTotal();
//clear message and remove class style
var alerts = document.getElementById("alerts");
alerts.innerHTML = "";
if(alerts.classList.contains("message")){
alerts.classList.remove("message");
}
}
}