forked from Anushkabh/krishiconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.js
126 lines (99 loc) · 3.35 KB
/
shop.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
117
118
119
120
121
122
123
124
125
126
// Add to Wishlist
function addToWishlist(name, price) {
var wishlistItems = JSON.parse(localStorage.getItem("wishlistItems")) || [];
var existingItem = wishlistItems.find(function (item) {
return item.name === name;
});
if (existingItem) {
return;
}
var newItem = {
name: name,
price: price,
};
wishlistItems.push(newItem);
localStorage.setItem("wishlistItems", JSON.stringify(wishlistItems));
updateDropdownWishlist(wishlistItems);
alert("Item added to wishlist successfully!");
Toastify({
text: "Product added to wishlist!",
duration: 5000,
gravity: "top",
innerHeight: 50,
position: "right",
backgroundColor: "rgba(0,128,0,0.8)",
}).showToast();
}
function updateDropdownWishlist(items) {
var dropdownContent = document.getElementById("wishlistDropdownContent");
dropdownContent.innerHTML = "";
items.forEach(function (item) {
var listItem = document.createElement("div");
listItem.classList.add("wishlist-item");
var itemName = document.createElement("span");
itemName.textContent = item.name + " - ₹" + item.price;
itemName.classList.add("item-name");
listItem.appendChild(itemName);
var addToCartButton = document.createElement("button");
addToCartButton.textContent = "Add to Cart";
addToCartButton.classList.add("add-to-cart-button");
addToCartButton.addEventListener("click", function () {
addToCart(item.name, item.price);
});
listItem.appendChild(addToCartButton);
var removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.classList.add("remove-button");
removeButton.addEventListener("click", function () {
removeFromWishlist(item.name);
});
listItem.appendChild(removeButton);
dropdownContent.appendChild(listItem);
});
var viewWishlistLink = document.createElement("a");
viewWishlistLink.textContent = "View Wishlist";
viewWishlistLink.href = "wishlist.html";
viewWishlistLink.classList.add("view-wishlist-link");
dropdownContent.appendChild(viewWishlistLink);
var wishlistCount = items.length;
var wishlistCountElement = document.getElementById("wishlistCount");
wishlistCountElement.textContent = wishlistCount;
}
function removeFromWishlist(name) {
var wishlistItems = JSON.parse(localStorage.getItem("wishlistItems")) || [];
wishlistItems = wishlistItems.filter(function (item) {
return item.name !== name;
});
localStorage.setItem("wishlistItems", JSON.stringify(wishlistItems));
updateDropdownWishlist(wishlistItems);
}
// Add to Cart
function addToCart(name, price) {
var cartItems = JSON.parse(localStorage.getItem("cartItems")) || [];
var existingItem = cartItems.find(function (item) {
return item.name === name;
});
if (existingItem) {
existingItem.quantity++;
} else {
cartItems.push({
name: name,
price: price,
quantity: 1,
});
}
localStorage.setItem("cartItems", JSON.stringify(cartItems));
// Show success toast
Toastify({
text: "Product added to cart!",
duration: 5000,
gravity: "top",
innerHeight: 50,
position: "right",
backgroundColor: "rgba(0,128,0,0.8)",
}).showToast();
}
// Initial update of wishlist on page load
var initialWishlistItems =
JSON.parse(localStorage.getItem("wishlistItems")) || [];
updateDropdownWishlist(initialWishlistItems);