-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.js
More file actions
140 lines (116 loc) · 4.88 KB
/
checkout.js
File metadata and controls
140 lines (116 loc) · 4.88 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
140
const SHIPPING_COST = 5.99;
const TAX_RATE = 0.1;
// Load order summary
function loadOrderSummary() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
if (cart.length === 0) {
alert('Your cart is empty!');
window.location.href = 'cart.html';
return;
}
const subtotal = cart.reduce((sum, item) => sum + parseFloat(item.price), 0);
const shipping = SHIPPING_COST;
const tax = (subtotal + shipping) * TAX_RATE;
const total = subtotal + shipping + tax;
let html = '';
cart.forEach(item => {
html += `
<div class="summary-item">
<span>${item.product}</span>
<span>$${item.price}</span>
</div>
`;
});
html += `
<div class="summary-item">
<span>Subtotal</span>
<span>$${subtotal.toFixed(2)}</span>
</div>
<div class="summary-item">
<span>Shipping</span>
<span>$${shipping.toFixed(2)}</span>
</div>
<div class="summary-item">
<span>Tax</span>
<span>$${tax.toFixed(2)}</span>
</div>
<div class="summary-item">
<span>Total</span>
<span>$${total.toFixed(2)}</span>
</div>
`;
document.getElementById('orderSummary').innerHTML = html;
// Store total for submission
window.orderTotal = total.toFixed(2);
}
// Format card number with spaces
document.getElementById('cardNumber').addEventListener('input', function(e) {
let value = e.target.value.replace(/\s/g, '');
let formatted = value.match(/.{1,4}/g)?.join(' ') || value;
e.target.value = formatted;
});
// Format expiry date
document.getElementById('expiry').addEventListener('input', function(e) {
let value = e.target.value.replace(/\D/g, '');
if (value.length >= 2) {
value = value.substring(0, 2) + '/' + value.substring(2, 4);
}
e.target.value = value;
});
// Handle payment method change
document.querySelectorAll('input[name="payment"]').forEach(radio => {
radio.addEventListener('change', function() {
document.getElementById('cardDetails').style.display =
this.value === 'credit' || this.value === 'debit' ? 'block' : 'none';
});
});
// Handle checkout form submission
function handleCheckout(e) {
e.preventDefault();
// Validate form
const firstName = document.getElementById('firstName').value;
const lastName = document.getElementById('lastName').value;
const email = document.getElementById('email').value;
if (!firstName || !lastName || !email) {
alert('Please fill in all required fields');
return;
}
const submitBtn = document.getElementById('submitBtn');
const form = document.getElementById('checkoutForm');
const successMessage = document.getElementById('successMessage');
// Simulate order processing
submitBtn.disabled = true;
submitBtn.textContent = 'Processing Order...';
setTimeout(() => {
// Clear cart
localStorage.setItem('cart', JSON.stringify([]));
// Show success message
form.style.display = 'none';
successMessage.classList.add('show');
// Scroll to success message
setTimeout(() => {
window.scrollTo({ top: 0, behavior: 'smooth' });
}, 500);
}, 2000);
}
// Scroll progress
window.addEventListener('scroll', () => {
const scrolled = (window.scrollY / (document.documentElement.scrollHeight - window.innerHeight)) * 100;
document.getElementById('scrollProgress').style.width = scrolled + '%';
});
// Initialize
loadOrderSummary();
// ================= MOBILE MENU (Hamburger) =================
const hamburger = document.getElementById('hamburger');
const mobileMenu = document.getElementById('mobileMenu');
if (hamburger && mobileMenu) {
hamburger.addEventListener('click', () => {
mobileMenu.classList.toggle('active');
});
// Close menu on link click
mobileMenu.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.remove('active');
});
});
}