-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
140 lines (129 loc) · 6.09 KB
/
cart.php
File metadata and controls
140 lines (129 loc) · 6.09 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
<?php
session_start();
include_once("header.php");
include('hms/include/config.php');
?>
<?php if (isset($_SESSION["showAlert"])): ?>
<div class="alert alert-success alert-dismissible mt-2">
<button type='button' class='close' data-dismiss='alert'>×</button>
<strong><?= htmlspecialchars($_SESSION["message"]) ?></strong>
</div>
<?php unset($_SESSION["showAlert"], $_SESSION["message"]); endif; ?>
<div class="container">
<div class="row">
<div class="col-12">
<div class="table-responsive">
<table class="table table-bordered table-striped text-center">
<thead class="thead-dark">
<tr>
<th>Product</th>
<th>MRP</th>
<th>Purchase Price</th>
<th>Discount</th>
<th>Quantity</th>
<th>Total Price</th>
<th>
<a href="action.php?clear=all" onClick="return confirm('Are you sure to clear cart?');" class="btn btn-sm btn-danger">Empty Cart</a>
</th>
</tr>
</thead>
<tbody>
<?php
$grand_total = 0;
$cart_items = mysqli_query($con, "SELECT * FROM cart");
if (mysqli_num_rows($cart_items) > 0):
while ($row = mysqli_fetch_assoc($cart_items)) {
$mrp = $row['proprice'];
$discounted = $row['prodiscount'];
$qty = $row['quantity'];
$discount = $mrp - $discounted;
$total_price = $discounted * $qty;
$grand_total += $total_price;
?>
<tr data-id="<?= $row['cart_id'] ?>">
<td>
<img src="hms/images/<?= htmlspecialchars($row['proimg']) ?>" width="80" height="80" alt="Image"><br>
<?= htmlspecialchars($row['proname']) ?>
</td>
<td>Rs. <?= number_format($mrp, 2) ?></td>
<td>Rs. <?= number_format($discounted, 2) ?></td>
<td>Rs. <?= number_format($discount, 2) ?></td>
<td>
<input type="number" class="form-control itemQty text-center" value="<?= $qty ?>" min="1" style="width:80px;">
<input type="hidden" class="pid" value="<?= $row['cart_id'] ?>">
<input type="hidden" class="pprice" value="<?= $discounted ?>">
</td>
<td class="itemTotal">Rs. <?= number_format($total_price, 2) ?></td>
<td>
<a href="action.php?remove=<?= $row['cart_id'] ?>" onClick="return confirm('Remove this item?');" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4"></td>
<td><strong>Subtotal</strong></td>
<td colspan="2"><strong id="subtotal">Rs. <?= number_format($grand_total, 2) ?></strong></td>
</tr>
<tr>
<td colspan="4"></td>
<td><strong>Total</strong></td>
<td colspan="2"><strong id="total">Rs. <?= number_format($grand_total, 2) ?></strong></td>
</tr>
<?php else: ?>
<tr>
<td colspan="7">
<h4 class="text-center text-muted">Your cart is empty!</h4>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<div class="col mb-2">
<div class="row">
<div class="col-sm-12 col-md-6">
<a href="index.php" class="btn btn-block btn-light"><i class="fa fa-shopping-cart"></i> Continue Shopping</a>
</div>
<div class="col-sm-12 col-md-6 text-right">
<a href="checkout.php" class="btn btn-md btn-block btn-success text-uppercase <?= ($grand_total > 0) ? '' : 'disabled'; ?>" style="background-color:pink;color:black;font-weight:bold;">Checkout</a>
</div>
</div>
</div>
</div>
</div>
<!-- Update Quantity Script -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
// change is an Event
$('.itemQty').on('change', function () {
const $row = $(this).closest('tr');
const pid = $row.find('.pid').val();
const pprice = parseFloat($row.find('.pprice').val());
let qty = parseInt($(this).val());
if (qty <= 0 || isNaN(qty)) {
qty = 1;
$(this).val(1);
}
$.ajax({
url: 'action.php',
method: 'POST',
data: { pqty: qty, pid: pid, pprice: pprice },
success: function () {
const itemTotal = (pprice * qty).toFixed(2);
$row.find('.itemTotal').text('Rs. ' + itemTotal);
// Recalculate grand total
let newTotal = 0;
$('.itemTotal').each(function () {
const amount = parseFloat($(this).text().replace('Rs. ', ''));
if (!isNaN(amount)) newTotal += amount;
});
$('#subtotal, #total').text('Rs. ' + newTotal.toFixed(2));
}
});
});
});
</script>
<?php include_once("footer.php"); ?>
<!-- cart.php file -->