i have a problem in php e commerce website that when product add from category A when move to category B here Category A data show on click cart side bar butt not update.when navigate between categories the button that show cart.
Welcome to the forum. Are you able to link to your code?
<style>
/* Order Submit Button */
.order-submit {
width: 100%;
background-color: green;
color: white;
border: none;
padding: 15px;
cursor: pointer;
font-size: 16px;
border-radius: 5px;
margin-top: 20px;
}
.order-submit:disabled {
background-color: #ccc;
cursor: not-allowed;
}
/* Cart Table Styling */
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
table th, table td {
padding: 10px;
border: 1px solid #ddd;
text-align: center;
}
table th {
background-color: #f4f4f4;
}
.action-buttons button {
background-color: green;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
margin: 2px;
}
.action-buttons button.minus {
background-color: red;
}
.action-buttons button:hover {
opacity: 0.8;
}
</style>
<?php
session_start();
require 'db.php';
// Debugging: Log the session data
error_log("Session data in cart_dropdown.php: " . print_r($_SESSION, true));
if (!isset($_SESSION['user_id'])) {
echo "Please log in to view the cart.";
exit();
}
$userId = $_SESSION['user_id'];
// Fetch the user's tariff
$stmt = $pdo->prepare("SELECT tariff_id FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch();
$tariffId = $user['tariff_id'];
// Debugging: Check the user's tariff ID
error_log("User ID: $userId, Tariff ID: $tariffId");
// Fetch cart items with tariff-specific prices
$cartItems = [];
$subtotal = 0;
if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $productId => $item) {
$stmt = $pdo->prepare("SELECT p.name, p.image, p.price, tp.price AS tariff_price
FROM products p
LEFT JOIN tariff_prices tp ON p.id = tp.product_id AND tp.tariff_id = ?
WHERE p.id = ?");
$stmt->execute([$tariffId, $productId]);
$product = $stmt->fetch();
if ($product) {
// Use tariff price if available, otherwise use default price
$price = $product['tariff_price'] ?? $product['price'];
$cartItems[$productId] = [
'name' => $product['name'],
'image' => $product['image'],
'price' => $price,
'quantity' => $item['quantity']
];
$subtotal += $price * $item['quantity'];
}
}
}
// Debugging: Check the cart items
error_log("Cart Items: " . print_r($cartItems, true));
// Calculate taxes and total
$taxes = 0;
$additionalTaxes = 0;
$total = $subtotal;
if ($tariffId) {
$stmt = $pdo->prepare("SELECT tax_rate, additional_tax_rate FROM tariffs WHERE id = ?");
$stmt->execute([$tariffId]);
$tariff = $stmt->fetch();
if ($tariff) {
$taxes = $subtotal * ($tariff['tax_rate'] / 100);
$additionalTaxes = $subtotal * ($tariff['additional_tax_rate'] / 100);
$total = $subtotal + $taxes + $additionalTaxes;
}
}
// Debugging: Check the subtotal, taxes, and total
error_log("Subtotal: $subtotal, Taxes: $taxes, Additional Taxes: $additionalTaxes, Total: $total");
?>
<table>
<tr>
<th>Image</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Action</th>
</tr>
<?php foreach ($cartItems as $productId => $item): ?>
<tr>
<td><img src="<?= $item['image'] ?>" alt="<?= $item['name'] ?>" style="width: 50px; height: auto;"></td>
<td><?= $item['name'] ?></td>
<td>$<?= number_format($item['price'], 2) ?></td>
<td><?= $item['quantity'] ?></td>
<td class="action-buttons">
<button onclick="updateCart(<?= $productId ?>, 'add')">+</button>
<button class="minus" onclick="updateCart(<?= $productId ?>, 'remove')">-</button>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>Subtotal: $<?= number_format($subtotal, 2) ?></p>
<p>Taxes: $<?= number_format($taxes, 2) ?></p>
<p>Additional Taxes: $<?= number_format($additionalTaxes, 2) ?></p>
<p>Total: $<?= number_format($total, 2) ?></p>
<!-- Submit Order Button -->
<div class="submit-order">
<form action="place_order.php" method="POST">
<button id="order-submit" class="order-submit" type="submit" <?= empty($cartItems) ? 'disabled' : '' ?>>Submit Order</button>
</form>
</div>