-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.html
130 lines (116 loc) · 6.9 KB
/
checkout.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment - Checkout </title>
<link rel="icon" type="image/png" href="/favicon.png">
<link rel="stylesheet" href="css/checkout.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
← <a href="shop.html" class="back-to-shop">Continue Shopping</a>
<hr>
<h1>Checkout</h1>
<div class="row">
<div class="col-75 checkout">
<div class="container">
<form action="shop.html" method="post" onsubmit="checkout()">
<div class="row">
<div class="col-50">
<h3>Billing Address</h3>
<label for="fname"><i class="fa fa-user"></i> Full Name *</label>
<input type="text" id="fname" name="firstname" placeholder="Chamod Karunathilake" autofocus="on" autocomplete="name" pattern="[A-Za-z]+" title="No numbers or special characters in the name" required>
<label for="email"><i class="fa fa-envelope"></i> Email *</label>
<input type="email" id="email" name="email" placeholder="[email protected]" autocomplete="email" required>
<label for="adr"><i class="fa fa-address-card-o"></i> Address *</label>
<input type="text" id="adr" name="address" placeholder="23 A. 7th Street" autocomplete="street-address" required>
<label for="city"><i class="fa fa-institution"></i> City *</label>
<input type="text" id="city" name="city" placeholder="Colombo" autocomplete="address-level2" required>
<div class="row">
<div class="col-50">
<label for="province">Province</label>
<input type="text" id="province" name="province" placeholder="Western">
</div>
<div class="col-50">
<label for="zip">Zip</label>
<input type="text" id="zip" name="zip" placeholder="10001">
</div>
</div>
</div>
<div class="col-50">
<h3>Payment</h3>
<label for="fname">Accepted Cards</label>
<div class="icon-container">
<i class="fa fa-cc-visa" style="color:navy;" title="Visa"></i>
<i class="fa fa-cc-amex" style="color:blue;" title="Amex"></i>
<i class="fa fa-cc-mastercard" style="color:red;" title="Mastercard"></i>
<i class="fa fa-cc-discover" style="color:orange;" title="Discover"></i>
</div>
<label for="cname">Name on Card *</label>
<input type="text" id="cname" name="cardname" placeholder="Chamod Karunathilake" pattern="[A-Za-z]+" title="No numbers or special characters in the name" required>
<label for="ccnum">Credit card number *</label>
<input type="text" id="ccnum" name="cardnumber" placeholder="1111-2222-3333-4444" required>
<label for="expmonth">Exp Month *</label>
<input type="text" id="expmonth" name="expmonth" placeholder="March" required>
<div class="row">
<div class="col-50">
<label for="expyear">Exp Year *</label>
<input type="text" id="expyear" name="expyear" placeholder="2024" required>
</div>
<div class="col-50">
<label for="cvv">CVV *</label>
<input type="text" id="cvv" name="cvv" placeholder="123" required>
</div>
</div>
</div>
</div>
<input type="submit" value="Continue to checkout" class="btn">
</form>
</div>
</div>
<div class="col-25 cart">
<div class="container">
<h4>Cart <span class="price" style="color:#fff"><i class="fa fa-shopping-cart"></i> <b class="cart-items-num"></b></span></h4>
<div class="cart-container"></div>
</div>
</div>
</div>
<script>
// Retrieve cart products from local storage
const cartProducts = JSON.parse(localStorage.getItem('cartProducts'));
// Select the container where cart items will be displayed
const displayCartItems = () => {
const cartContainer = document.querySelector('.cart-container');
cartProducts.forEach(product => {
const productElement = document.createElement('div');
productElement.classList.add('cart-item');
productElement.innerHTML = `
<p>${product.title} (${product.description}) <span>× ${product.quantity}</span><span class="price">$${(product.price * product.quantity).toFixed(2)}<span></p>
`;
cartContainer.appendChild(productElement);
});
// Calculate and display total price
const totalItems = cartProducts.reduce((acc, item) => acc + item.quantity, 0);
const totalPrice = cartProducts.reduce((acc, item) => acc + (item.price * item.quantity), 0);
const totalElement = document.createElement('div');
totalElement.classList.add('price');
totalElement.innerHTML = `
<hr>
<p>Total <span class="price"><b>$${totalPrice.toFixed(2)}</b></span></p>
`;
cartContainer.appendChild(totalElement);
document.querySelector('.cart-items-num').innerHTML = `${totalItems}`;
};
// Call the function to display cart items
displayCartItems();
const checkout = () => {
const totalItems = cartProducts.reduce((acc, item) => acc + item.quantity, 0);
const totalPrice = cartProducts.reduce((acc, item) => acc + (item.price * item.quantity), 0);
const fname = document.getElementById("fname").value;
alert("Order has been placed!\n"+"Summary:\nName: "+fname+"\nTotal items: "+totalItems+"\nTotal price: "+totalPrice);
localStorage.removeItem('cartProducts'); // Clear cart local storage
};
</script>
</body>
</html>