Hi Team
I need some help with my basket its adding the item, but on the basket.php shopping cart is empty.
What I have tried:
<div id="basket-overview" class="navbar-collapse collapse d-none d-lg-block">
<a href="basket.php" class="btn btn-primary navbar-btn">
class="fa fa-shopping-cart">
<?php echo isset($_SESSION['cart']) ? '<span id="cart-count">' . count($_SESSION['cart']) . '</span>' : ''; ?>
</a>
</div>
<div id="basket" class="col-lg-9">
<div class="box">
<form method="post" action="add_to_cart.php">
<h1>Shopping cart</h1>
<p class="text-muted">You currently have <span id="cart-count"><?php echo isset($_SESSION['cart']) ? '<span id="cart-count">' . count($_SESSION['cart']) . '</span>' : ''; ?>
<p id="cart-message" class="text-success" style="display: none;"></p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th colspan="2">Product</th>
<th>Quantity</th>
<th>Unit price</th>
<th>Discount</th>
<th colspan="2"><span id="total-amount">Total</span></th>
</tr>
</thead>
<tbody id="cart-items">
<tr>
<td><a href="#"><img src="img/detailsquare.jpg" alt="White Blouse Armani"></a></td>
<td><a href="#">White Blouse Armani</a></td>
<td>
<div class="input-group">
<button class="btn btn-default btn-minus" type="button">-</button>
<input type="number" name="product1" value="0" min="1" class="form-control product-quantity" data-product-id="1">
<button class="btn btn-default btn-plus" type="button">+</button>
</div>
// jquery code
function addToCart(productId, quantity) {
var cart = getCart();
if (cart.hasOwnProperty(productId)) {
var newQuantity = parseInt(quantity);
if (newQuantity <= 0) {
delete cart[productId];
} else {
cart[productId] = newQuantity;
}
} else {
cart[productId] = parseInt(quantity);
}
saveCart(cart);
updateCartCount();
calculate_total();
var productName = $('#product-name-' + productId).text();
$('#cart-message').text(quantity + 'x ' + productName + ' added to cart').show();
setTimeout(function() {
$('#cart-message').fadeOut('slow');
}, 3000);
}
function calculate_total() {
var cart = getCart();
var total = 0;
for (var productId in cart) {
var product = getProductId(productId);
var price = parseFloat(product.price);
var quantity = parseInt(cart[productId]);
$.ajax({
url: 'get_product_details.php',
data: {productId: productId},
dataType: 'json',
success: function(data) {
var discount = parseFloat(data.discount);
var amount = price * quantity * (1 - discount);
$('#discount-' + productId).text((discount * 100).toFixed(0) + '%');
$('#amount-' + productId).text('$' + amount.toFixed(0));
},
error: function(xhr, textStatus, errorThrown) {
console.log('Error getting product details: ' + errorThrown);
}
});
<?php
session_start();
echo count($_SESSION['cart']);
if (!empty($_SESSION['cart'])) {
$conn = mysqli_connect('localhost', 'root', '', 'ecommerce_store');
if (!$conn) {
die('Could not connect to database: ' . mysqli_connect_error());
}
$cart_items = array();
foreach ($_SESSION['cart'] as $product_id => $quantity) {
$sql = "SELECT * FROM products WHERE productID = '$product_id'";
$result = mysqli_query($conn, $sql);
if (!$result) {
die('Error getting product details: ' . mysqli_error($conn));
}
$product = mysqli_fetch_assoc($result);
$product['quantity'] = $quantity;
$cart_items[] = $product;
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Shopping Cart</h1>
<?php if (!empty($cart_items)): ?>
<table class="table">
<thead>
<tr>
<th>Image</th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Discount</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_items as $cart_item): ?>
<tr>
<td><img src="<?php echo $cart_item['image']; ?>" alt="<?php echo $cart_item['product_name']; ?>"></td>
<td><?php echo $cart_item['product_name']; ?></td>
<td>$<?php echo $cart_item['unit_price']; ?></td>
<td><?php echo $cart_item['quantity']; ?></td>
<td><?php echo $cart_item['discount']; ?></td>
<td>$<?php echo ($cart_item['unit_price'] * $cart_item['quantity'] * (1 - $cart_item['discount'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Your cart is empty.</p>
<?php endif; ?>
</div>
</body>
</html>