-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.php
133 lines (124 loc) · 6.02 KB
/
cart.php
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
<?php
session_start();
include 'database/db_connection.php';
if (isset($_GET['querySelectedItems'])) {
$productID = isset($_GET['productID']) ? $_GET['productID'] : -1;
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : -1;
$user_id = $_SESSION["user_id"];
$sql = "UPDATE cart_items SET product_quantity = $quantity WHERE user_id = $user_id AND product_id = $productID";
$result = $con->query($sql);
echo "<script>console.log('Quantity updated:',$quantity);</script>";
if (!$result) {
echo "<script>console.log('Error: $con->error')</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jack Daniels - Cart</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<?php
include 'components/navbar.php';
echo "<h2>Cart</h2>";
// check if the user has products in the cart
$user_id = $_SESSION["user_id"];
$sql = "SELECT * FROM cart_items WHERE user_id = $user_id";
$result = $con->query($sql);
$checkItems = 0; // count the number of items checked by the user
if ($result->num_rows > 0) {
// the user has products in the cart
while ($row = $result->fetch_assoc()) {
$productID = $row["product_id"];
$sql = "SELECT * FROM products WHERE product_id = $productID";
$product = $con->query($sql)->fetch_assoc();
$productName = $product["product_name"];
$productPrice = $product["product_price"];
$productQuantity = $row["product_quantity"];
$productDescription = $product["product_description"];
$productImageURL = $product["product_image_url"];
include "components/display/cart_item.php";
}
echo "<p>Total: $<span id='total-price'>0.00</span></p>";
echo "<p>Total Items: <span class='total'>$checkItems</span></p>";
echo "<button id='checkout-button' onclick='checkout()'>Checkout ($checkItems)</button>";
} else {
echo "<p>Your cart is empty.</p>";
}
?>
<?php
include 'components/footer.php';
?>
</body>
<script src="index.js"></script>
<script src="shop_helper.js"></script>
<script>
// get the ID of the div whose child was clicked
document.addEventListener("click", function (e) {
if (e.target.classList.contains("increment-button")) {
// increment the quantity
const quantity = e.target.parentElement.querySelector(".quantity");
quantity.innerText = parseInt(quantity.innerText) + 1;
// update the total price
const productID = e.target.parentElement.id.replace("quantity-control", "");
const productPrice = document.querySelector(`#product-price${productID}`).innerText;
if (document.querySelector(`#cart-item${productID}`).querySelector(".checkout-box").checked) {
const totalPrice = document.querySelector("#total-price");
totalPrice.innerText = parseFloat(totalPrice.innerText) + parseFloat(productPrice);
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "helpers/shop/checkout.php?queryType=updateCart&productID=" + productID + "&quantity=" + quantity.innerText, true);
xhttp.send();
} else if (e.target.classList.contains("decrement-button")) {
// decrement the quantity
const quantity = e.target.parentElement.querySelector(".quantity");
if (parseInt(quantity.innerText) > 1) {
quantity.innerText = parseInt(quantity.innerText) - 1;
// update the total price
const productID = e.target.parentElement.id.replace("quantity-control", "");
const productPrice = document.querySelector(`#product-price${productID}`).innerText;
if (document.querySelector(`#cart-item${productID}`).querySelector(".checkout-box").checked) {
const totalPrice = document.querySelector("#total-price");
totalPrice.innerText = parseFloat(totalPrice.innerText) - parseFloat(productPrice);
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "helpers/shop/checkout.php?queryType=updateCart&productID=" + productID + "&quantity=" + quantity.innerText, true);
xhttp.send();
}
}
});
// update the total price when the checkbox is checked
document.addEventListener("change", function (e) {
if (e.target.classList.contains("checkout-box")) {
const checkbox = e.target;
const quantity = checkbox.parentElement.querySelector(".quantity");
const productID = checkbox.parentElement.id.replace("cart-item", "");
const productPrice = document.querySelector(`#product-price${productID}`).innerText;
const totalPrice = document.querySelector("#total-price");
const totalItems = document.querySelector(".total");
if (checkbox.checked) {
totalPrice.innerText = parseFloat(totalPrice.innerText) + (parseFloat(productPrice) * parseInt(quantity.innerText));
totalItems.innerText = parseInt(totalItems.innerText) + 1;
} else {
totalPrice.innerText = parseFloat(totalPrice.innerText) - (parseFloat(productPrice) * parseInt(quantity.innerText));
totalItems.innerText = parseInt(totalItems.innerText) - 1;
}
document.querySelector("#checkout-button").innerText = `Checkout (${totalItems.innerText})`;
}
});
</script>
</html>