-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.html
197 lines (176 loc) · 10.7 KB
/
cart.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cart</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!--Header-->
<div id="parent">
<div class="logo"><img src="Images/logo web.png" alt="Comp"></div>
</div>
<!--Products Display-->
<div class="cartBox">
<div class="box">
<h3 style="color: white;display: flex;font-size: 22px;font-weight: lighter;">Review Your Order | Total Items(<div id="number" style="color:#f19b1a;"></div>) </h3>
<div id="cartItems"></div>
<div class="empty" style="display: flex; align-items: center; justify-content: center;">
<div style="display: block; text-align: center;">
<img src="Images/Screenshot 2024-07-04 224703.png" style="height: 200px; width: 400px; margin: 0;">
<p style="color:#ee842e;; font-size: 22px; margin-top: 0;">Your Cart is Empty!</p>
</div>
</div>
<button onclick="resetCart()" class="reset">Reset Cart</button>
</div>
<!--Price Display-->
<div id="total-price"></div>
</div>
<script>
// Reset
function resetCart() {
localStorage.removeItem("Cart");
localStorage.removeItem("count");
location.reload(); // Reload the page to reflect changes
}
let totalPrice;
let cart;
//Display Products Added to Cart
document.addEventListener("DOMContentLoaded", function() {
cart = JSON.parse(localStorage.getItem("Cart")) || [];
const cartItemsDiv = document.getElementById("cartItems");
const totalPriceDiv = document.getElementById("total-price");
const number = document.getElementById("number");
totalPrice = 0;
cart.forEach(item => {
const itemLi = document.createElement("div");
const itemDetails = document.createElement("div");
itemDetails.className = "newCartItem";
itemDetails.innerHTML = `<div>
<img src="${item.proImage}" class="itemimg">
</div>
<div class="itemDetails">
<p id="name">${item.proName}</p>
<p id="price"> ₹${item.proPrice}</p>
<div class="quanta"><div class="but">Quantity: ${item.proQuantity} </div><button class="button">Update</button> <button class="delete">Delete</button></div>
</div>`;
itemLi.appendChild(itemDetails);
cartItemsDiv.appendChild(itemLi);
let withoutComma = item.proPrice.replace(',','');
totalPrice += parseInt(withoutComma)*item.proQuantity; //1
document.querySelector('.empty').style.display = 'none';
});
//UpdateQuty();
document.querySelectorAll('.button').forEach((buton , index)=>{
buton.addEventListener('click', ()=>{
let boy;
const df = buton.parentElement;
const hi = df.querySelector('.but');
hi.innerHTML = `Quantity: <select class="sel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<button id="save">Save</button>`
const sel = document.querySelector('.sel');
sel.addEventListener('change', ()=>{
cart[index].proQuantity = document.querySelector('.sel').value;
});
const savee = document.querySelector('#save');
savee.addEventListener('click', ()=>{
hi.innerHTML = `Quantity: ${cart[index].proQuantity}`
update(); // Function to Display Updated Prices
console.log(totalPrice);
});
});
});
//Delete Product
let cou = 0;
document.querySelectorAll('.delete').forEach((del , index)=>{
del.addEventListener('click' , ()=>{
cart.splice(index , 1);
localStorage.setItem("Cart", JSON.stringify(cart));
console.log(cart);
location.reload(); // Reload the page to reflect changes
});
});
//Update Price Shown
function update(){
totalPrice = 0;
cart.forEach(item => {
let withoutComma = item.proPrice.replace(',', '');
totalPrice += parseInt(withoutComma) * item.proQuantity;
});
console.log(totalPrice); // Log totalPrice after updating
totalPriceDiv.innerHTML = `<div class="order">Order Summary</div>
<div style="margine-top: 60px;">Total Price : <div id="price1">₹${totalPrice}</div></div>
<div>Shipping & Handling : <div id="price1">₹0</div></div>
<div style="border-bottom: 1px solid rgb(153, 152, 152);">After GST (10%) : <div id="price1">₹${(totalPrice * 0.1) + totalPrice}</div></div>
<div style="color:#dd3030; font-size: 20px; font-weight: bold;">SubTotal : <div id="price1" style="font-size: 20px; font-weight: bold;">₹${((((parseInt((totalPrice*0.1) + totalPrice))/1000).toFixed(3)).toString()).replace('.',',')}</div></div>
<center><form class="placeOrder"><button>Place Order</button></form></center>
<center><div class="secure">
<img src="Images/shieldTick.png">
<p style="color:#878787; font-size: 13px;">
Safe and Secure Payments.Easy returns.100% Authentic products.
</p>
</div></center>`;
const csrpForm = document.querySelector('.placeOrder');
asdf(csrpForm);
};
number.textContent = ` ${cart.length} `;
totalPriceDiv.innerHTML = `<div class="order">Order Summary</div>
<div style="margine-top: 60px;">Total Price : <div id="price1">₹${totalPrice}</div></div>
<div>Shipping & Handling : <div id="price1">₹0</div></div>
<div style="border-bottom: 1px solid rgb(153, 152, 152);">After GST (10%) : <div id="price1">₹${(totalPrice * 0.1) + totalPrice}</div></div>
<div style="color:#dd3030; font-size: 20px; font-weight: bold;">SubTotal : <div id="price1" style="font-size: 20px; font-weight: bold;">₹${((((parseInt((totalPrice*0.1) + totalPrice))/1000).toFixed(3)).toString()).replace('.',',')}</div></div>
<center><form class="placeOrder"><button>Place Order</button></form></center>
<center><div class="secure">
<img src="Images/shieldTick.png">
<p style="color:#878787; font-size: 13px;">
Safe and Secure Payments.Easy returns.100% Authentic products.
</p>
</div></center>`;
const csrpiForm = document.querySelector('.placeOrder');
asdf(csrpiForm);
});
function asdf(csrpForm){
csrpForm.addEventListener('submit', async (e) => {
e.preventDefault();
let OrderPriced = (totalPrice * 0.1) + totalPrice;
let csrs = JSON.parse(localStorage.getItem("csrs"));
console.log(csrs.email);
let csrsEmail = csrs.email;
try {
const response = await fetch('http://localhost:8002/user/order', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
cEmail : csrsEmail,
cartItm : cart,
OrderPrice : OrderPriced
}),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const result = await response.json();
window.location.href= 'sriram4.html';
console.log('Success:', result);
csrForm.reset();
} catch (error) {
console.error('Error:', error);
}
});
};
</script>
</body>
</html>