-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfood.js
More file actions
139 lines (122 loc) · 4.23 KB
/
food.js
File metadata and controls
139 lines (122 loc) · 4.23 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
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
const product = [
{
id: 0,
image: 'pics/chapati.jpeg',
title: 'Chapati Plain',
price: 15,
},
{
id: 1,
image: 'pics/chips.jpeg',
title: 'Chips Plain',
price: 80,
},
{
id: 2,
image: 'pics/hotdog.jpg',
title: 'Hotdogs',
price: 60,
},
{
id: 3,
image: 'pics/byriani.jpeg',
title: 'Byriani',
price: 80,
}
];
const categories = [...new Set(product.map((item)=>
{return item}))]
let i=0;
document.getElementById('root').innerHTML = categories.map((item) =>
{
var {image, title, price} = item;
return(
`<div class='box'>
<div class='img-box'>
<img class='images' src=${image}></img>
</div>
<div class='bottom'>
<p>${title}</p>
<h2>Ksh ${price}.00</h2>`+
"<button onclick='addtocart("+(i++)+")'>Add to cart</button>"+
`</div>
</div>`
)
}).join('')
var cart = [];
function addtocart(a){
cart.push({...categories[a]});
displayCart();
}
function delElement(a){
cart.splice(a, 1);
displayCart();
}
function displayCart(a){
let j = 0, total = 0;
document.getElementById("count").innerHTML = cart.length;
if (cart.length==0){
document.getElementById('cartItem').innerHTML = 'Your cart is empty';
document.getElementById("total").innerHTML = "Ksh "+0+".00";
}
else{
document.getElementById("cartItem").innerHTML = cart.map((items) =>
{
var {image, title, price} = items;
total=total+price;
document.getElementById("total").innerHTML = "Ksh "+total+".00";
return(
`<div class='cart-item'>
<div class='row-img'>
<img class='rowimg' src=${image}>
</div>
<p style='font-size:13px;'>${title}</p>
<h2 style='font-size:15px;'>Ksh ${price}.00</h2>`+
"<i class='fa-solid fa-trash' onclick='delElement("+ (j++) +")'></i></div>"
);
}).join('')
}
}
});
function addToCart(item) {
// Retrieve the current cartitems from localStorage
let cart = JSON.parse(localStorage.getItem('cart')) || [];
// Add the new item to the cart
cart.push(item);
// Store the updated cart back into the localStorage
localStorage.setItem('cart', JSON.stringify(cart));
}
// Function to display the cart items
function displayCartItems() {
const cartItemsContainer = document.getElementById('cartItems');
const totalPriceElement = document.getElementById('total');
let total = 0;
// Retrieve cart items from localStorage
const cart = JSON.parse(localStorage.getItem('cart')) || [];
// Clear any previous cart items
cartItemsContainer.innerHTML = '';
// If cart is empty, display a message
if (cart.length === 0) {
cartItemsContainer.innerHTML = '<p>No items in your cart.</p>';
totalPriceElement.textContent = '0.00';
return;
}
// Loop through cart items and display them
cart.forEach(item => {
const itemElement = document.createElement('div');
itemElement.classList.add('cart-item');
itemElement.innerHTML = <p>${item.name} - $${item.price.toFixed(2)}</p>;
cartItemsContainer.appendChild(itemElement);
total += item.price;
});
// Update the total price
totalPriceElement.textContent = total.toFixed(2);
}
// Payment button click handler
document.getElementById('payment-button').addEventListener('click', () => {
alert('Redirecting to payment gateway...');
// Redirect to a payment processing page
window.location.href = "thank-you.html"; // Redirect to a thank you page after payment
});
// Initialize cart display
displayCartItems();