-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscript.js
95 lines (59 loc) · 2.49 KB
/
script.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
const cartItems = {}; // Object to store item details
//Added Event Listener to Carts
document.addEventListener('click', function (event) {
const clickedElement = event.target.closest('.add');
if (clickedElement) {
const itemName = clickedElement.getAttribute('data-name');
const itemPrice = parseFloat(clickedElement.getAttribute('data-price'));
// Update or add item details in the cartItems object
if (cartItems[itemName]) {
cartItems[itemName].quantity += 1;
}
else {
cartItems[itemName] = {
price: itemPrice,
quantity: 1
};
updateItemNumber(); // Update item number when a new item is added
}
}
});
//Built Update Function
function updateItemNumber() {
const itemCount = document.getElementById('item-count');
let currentItemCount = parseInt(itemCount.textContent);
currentItemCount += 1;
itemCount.textContent = currentItemCount;
}
//Added Event Listener to the Main Cart
const cart = document.getElementById('cart');
const cartPopup = document.getElementById("cart-popup");
const checkoutItems = document.getElementById("checkout-items")
const bill = document.getElementById("total-amount")
const checkout = document.querySelector("#checkout")
let amountTotal = 0;
cart.addEventListener('click', function () {
cartPopup.classList.toggle("show")
checkoutItems.innerHTML = ''; // Clear the existing content in checkoutItems
amountTotal = 0 // Initialize the total amount
for (const [itemTitle, itemInfo] of Object.entries(cartItems)) {
checkoutItems.innerHTML += `<p> ${itemTitle} : ${itemInfo.price} (${itemInfo.quantity}) </p>`
// Calculate item total and add it to the total amount
const itemTotal = itemInfo.price * itemInfo.quantity;
amountTotal += itemTotal;
}
bill.innerHTML = amountTotal
whatsappLink = "https://api.whatsapp.com/send?phone=918114720014&text=Order%20details"
whatsappApi()
});
//Whatsapp Api
let whatsappLink = "https://api.whatsapp.com/send?phone=918114720014&text=Order%20details"
function whatsappApi() {
for (const [itemTitle, itemInfo] of Object.entries(cartItems)) {
whatsappLink += "%0A" + itemTitle + ":" + "%20" + "Rs." + itemInfo.price + "%20" + "(" + itemInfo.quantity + ")"
}
whatsappLink += "%0A" + "The total amount is Rs." + amountTotal
}
checkout.addEventListener("click", () => {
window.open(whatsappLink)
})