-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.html
More file actions
98 lines (92 loc) · 3.15 KB
/
Copy pathcart.html
File metadata and controls
98 lines (92 loc) · 3.15 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
background-color: #f4f4f4;
}
.cart-container {
max-width: 400px;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
ul {
list-style: none;
padding: 0;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #ddd;
}
.delete-btn {
background: red;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
border-radius: 4px;
}
.delete-btn:hover {
background: darkred;
}
#checkout {
width: 100%;
padding: 10px;
background: green;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
margin-top: 10px;
}
#checkout:hover {
background: darkgreen;
}
</style>
</head>
<body>
<section class="cart-container">
<h2>Shopping Cart</h2>
<ul id="cart-list"></ul>
<p><strong>Delivery Price:</strong> <span id="delivery-price">10.1</span> TUB</p>
<p><strong>Total:</strong> <span id="total-price">0</span> TUB</p>
<button id="checkout">Checkout</button>
</section>
<script>
let cart = JSON.parse(localStorage.getItem('cart')) || [];
const deliveryPrice = 10.1; // سعر التوصيل
function updateCart() {
let cartList = document.getElementById('cart-list');
let totalPrice = document.getElementById('total-price');
cartList.innerHTML = '';
let total = 0;
cart.forEach((item, index) => {
let li = document.createElement('li');
li.innerHTML = `${item.name} - ${item.price} TUB
<button class="delete-btn" onclick="removeFromCart(${index})">X</button>`;
cartList.appendChild(li);
total += parseFloat(item.price); // جمع الأسعار
});
total += deliveryPrice; // إضافة سعر التوصيل إلى المجموع الكلي
totalPrice.textContent = total.toFixed(2); // عرض المجموع مع منزلتين عشريتين
localStorage.setItem('cart', JSON.stringify(cart)); // تحديث localStorage
}
function removeFromCart(index) {
cart.splice(index, 1); // حذف العنصر من المصفوفة
updateCart(); // تحديث السلة
}
updateCart(); // استدعاء الدالة لتحديث السلة عند تحميل الصفحة
</script>
</body>
</html>