���� JFIF �� � ( %"1"%)+...383,7(-.-
![]() Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20 System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 User : apache ( 48) PHP Version : 7.4.20 Disable Function : NONE Directory : /proc/self/root/var/www/html/tien-dien/code/cart/ |
<?php global $DB, $INFO, $print, $v_lang, $CORE; // Initialize session if not already started if (session_status() == PHP_SESSION_NONE) { session_start(); } // Initialize cart if it doesn't exist if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } // Handle cart actions $action = isset($_GET['action']) ? $_GET['action'] : ''; $product_id = isset($_GET['id']) ? intval($_GET['id']) : 0; $quantity = isset($_GET['quantity']) ? intval($_GET['quantity']) : 1; switch ($action) { case 'add': if ($product_id > 0) { // Get product details $product = $DB->fetch_row($DB->query("SELECT * FROM tb_product WHERE id='$product_id' AND status=0")); if ($product && $product['price'] > 0) { // Check if product already exists in cart $found = false; foreach ($_SESSION['cart'] as $key => $item) { if ($item['id'] == $product_id) { $_SESSION['cart'][$key]['quantity'] += $quantity; $found = true; break; } } // If product not in cart, add it if (!$found) { $price = getDiscountedPrice($product); $_SESSION['cart'][] = array( 'id' => $product_id, 'title' => $product['title'], 'price' => $price, 'quantity' => $quantity, 'path_img' => $product['path_img'] ); } // Redirect back to cart $print->refresh('?act=cart'); } } break; case 'update': if ($product_id > 0 && $quantity > 0) { foreach ($_SESSION['cart'] as $key => $item) { if ($item['id'] == $product_id) { $_SESSION['cart'][$key]['quantity'] = $quantity; break; } } } $print->refresh('?act=cart'); break; case 'remove': if ($product_id > 0) { foreach ($_SESSION['cart'] as $key => $item) { if ($item['id'] == $product_id) { unset($_SESSION['cart'][$key]); $_SESSION['cart'] = array_values($_SESSION['cart']); // Reindex array break; } } } $print->refresh('?act=cart'); break; case 'clear': $_SESSION['cart'] = array(); $print->refresh('?act=cart'); break; } // Calculate cart totals $subtotal = 0; $total_items = 0; foreach ($_SESSION['cart'] as $item) { $subtotal += $item['price'] * $item['quantity']; $total_items += $item['quantity']; } $total = $subtotal; // Can add shipping, tax, etc. here if needed $CORE->title_page = 'Giỏ hàng | '; include 'layout/'.$INFO['path_skin'].'/header.php'; include 'layout/'.$INFO['path_skin'].'/menu.php'; ?> <div class="container mt-4 mb-4"> <div class="row"> <div class="col-md-12"> <h1 class="page-title">Giỏ hàng</h1> </div> </div> <?php if (empty($_SESSION['cart'])): ?> <div class="row"> <div class="col-md-12"> <div class="alert alert-info"> Giỏ hàng của bạn đang trống. <a href="?act=product">Tiếp tục mua sắm</a> </div> </div> </div> <?php else: ?> <div class="row"> <div class="col-md-9"> <div class="card"> <div class="card-body"> <div class="table-responsive"> <table class="table table-hover"> <thead> <tr> <th></th> <th>Sản phẩm</th> <th>Giá</th> <th>Số lượng</th> <th>Tổng</th> <th></th> </tr> </thead> <tbody> <?php foreach ($_SESSION['cart'] as $item): ?> <tr> <td> <?php if($item['path_img']): ?> <img src="<?php echo $item['path_img']; ?>" alt="<?php echo $item['title']; ?>" style="max-width: 80px;"> <?php endif; ?> </td> <td> <a href="?act=product&type=detail&id=<?php echo $item['id']; ?>"><?php echo $item['title']; ?></a> </td> <td><?php echo number_format($item['price']); ?> VNĐ</td> <td> <div class="input-group" style="max-width: 120px;"> <input type="number" class="form-control" value="<?php echo $item['quantity']; ?>" min="1" id="quantity_<?php echo $item['id']; ?>"> <div class="input-group-append"> <button class="btn btn-outline-secondary" type="button" onclick="updateQuantity(<?php echo $item['id']; ?>)"> <i class="fa fa-refresh"></i> </button> </div> </div> </td> <td><?php echo number_format($item['price'] * $item['quantity']); ?> VNĐ</td> <td> <a href="?act=cart&action=remove&id=<?php echo $item['id']; ?>" class="btn btn-danger btn-sm"> <i class="fa fa-trash"></i> </a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <div class="card-footer text-right"> <a href="?act=product" class="btn btn-secondary">Tiếp tục mua sắm</a> <a href="?act=cart&action=clear" class="btn btn-danger">Xóa giỏ hàng</a> </div> </div> </div> <div class="col-md-3"> <div class="card"> <div class="card-header"> <h5>Tổng giỏ hàng</h5> </div> <div class="card-body"> <table class="table"> <tr> <td>Tổng sản phẩm:</td> <td class="text-right"><?php echo $total_items; ?></td> </tr> <tr> <td>Tạm tính:</td> <td class="text-right"><?php echo number_format($subtotal); ?> VNĐ</td> </tr> <tr> <th>Tổng cộng:</th> <th class="text-right"><?php echo number_format($total); ?> VNĐ</th> </tr> </table> </div> <div class="card-footer"> <a href="?act=checkout" class="btn btn-primary btn-block">Tiến hành thanh toán</a> </div> </div> </div> </div> <?php endif; ?> </div> <script> function updateQuantity(productId) { var quantity = document.getElementById('quantity_' + productId).value; if (quantity > 0) { window.location.href = '?act=cart&action=update&id=' + productId + '&quantity=' + quantity; } } </script> <?php include 'layout/'.$INFO['path_skin'].'/footer.php'; ?>