File manager - Edit - /var/www/order.cmtc.ac.th/user/index_4.php
Back
<?php session_start(); include('../config/db.php'); include('../includes/check_timer.php'); // ✅ ตรวจสอบว่ามี session การชำระเงินค้างอยู่หรือไม่ $checkout_locked = false; if (isset($_SESSION['checkout_expire']) && time() < $_SESSION['checkout_expire']) { $checkout_locked = true; // ยังอยู่ในระยะเวลาชำระเงิน } // ✅ ตรวจสอบว่ามีเวลานับถอยหลังจาก checkout.php หรือไม่ $checkout_active = false; if (isset($_SESSION['checkout_expire'])) { if (time() < $_SESSION['checkout_expire']) { $checkout_active = true; } else { // ถ้าหมดเวลาแล้วให้ล้างค่า unset($_SESSION['checkout_expire']); } } // ✅ โหลดข้อมูลการจองก่อนหน้า (ถ้ามี) $checkout_old = $_SESSION['checkout'] ?? [ 'fullname' => '', 'phone' => '', 'address' => '', 'zipcode' => '', 'receive_method' => '' ]; // ✅ ตรวจสอบสถานะระบบจอง $booking_row = $conn->query("SELECT setting_value FROM settings WHERE setting_key='booking_status'")->fetch_assoc(); $booking_status = $booking_row['setting_value'] ?? 'on'; // ✅ ตรวจสอบค่าสถานะ Free Order $setting = $conn->query("SELECT setting_value FROM settings WHERE setting_key='pre_order_status'")->fetch_assoc(); $pre_order_status = $setting['setting_value'] ?? 'off'; // ✅ ตรวจสอบว่ามีเหรียญในตะกร้า if(!isset($_SESSION['cart']) || count($_SESSION['cart']) == 0){ include('template_user_header.php'); echo "<div class='container py-5 text-center'> <h3>🛒 ยังไม่มีเหรียญในตะกร้า</h3> <a href='../index.php' class='btn btn-primary mt-3'>กลับไปเลือกเหรียญ</a> </div>"; exit; } // ✅ ลบเหรียญออกจากตะกร้า (คืน reserved) if(isset($_GET['remove'])){ $rid = intval($_GET['remove']); if(isset($_SESSION['cart'][$rid])){ $qty_remove = intval($_SESSION['cart'][$rid]); $conn->query("UPDATE products SET reserved = GREATEST(reserved - $qty_remove, 0) WHERE id = $rid"); unset($_SESSION['cart'][$rid]); } header("Location: index.php"); exit; } // ✅ ดึงข้อมูลเหรียญในตะกร้า $ids = implode(",", array_keys($_SESSION['cart'])); $res = $conn->query("SELECT id, name, price, stock, reserved, img1, img2 FROM products WHERE id IN ($ids)"); $cart_items = []; $total = 0; while($r = $res->fetch_assoc()){ $pid = $r['id']; $qty = $_SESSION['cart'][$pid]; $available = max(0, $r['stock'] - $r['reserved']); $subtotal = $r['price'] * $qty; $total += $subtotal; $cart_items[] = [ 'id' => $pid, 'name' => $r['name'], 'price' => $r['price'], 'stock' => $r['stock'], 'reserved' => $r['reserved'], 'available' => $available, 'img1' => $r['img1'], 'qty' => $qty, 'subtotal' => $subtotal ]; } ?> <?php include('template_user_header.php'); ?> <?php include('includes/timer_bar.php'); ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>🛒 ตะกร้าเหรียญ</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Kanit:wght@300;400;600&display=swap" rel="stylesheet"> <style> body,*{font-family:'Kanit',sans-serif!important;} .table td, .table th { vertical-align: middle; } img.thumb {width:80px;border-radius:8px;margin:2px;} .product-images {display:flex;justify-content:center;align-items:center;gap:5px;} .img-label {font-size:13px;color:#555;text-align:center;} .section-divider { border-top: 3px dashed #ccc; margin: 40px 0; } .stock-label { font-size: 13px; color: #666; } .qty-group { display: flex; justify-content: center; align-items: center; gap: 5px; } .qty-btn { width: 32px; height: 32px; border: 1px solid #ccc; background-color: #f8f9fa; border-radius: 6px; cursor: pointer; font-weight: bold; transition: 0.2s; } .qty-btn:hover { background-color: #e0e0e0; } .qty-input { width: 60px !important; text-align: center; border: 1px solid #ccc; border-radius: 6px; } </style> </head> <body class="bg-light"> <div class="container py-5"> <h3 class="text-center mb-4"> รายการเหรียญในตะกร้า</h3> <?php if($booking_status == 'off'): ?> <div class="alert alert-danger text-center fs-5 fw-bold"> ⛔ ขณะนี้ระบบปิดรับการจองเหรียญ </div> <?php endif; ?> <form action="checkout.php" method="post" id="cartForm"> <table class="table table-bordered bg-white align-middle table-responsive"> <thead class="table-secondary text-center"> <tr> <th>รูป</th> <th>ชื่อเหรียญ</th> <th>ราคา (บาท)</th> <th>จำนวน</th> <th>รวม</th> <th>ลบ</th> </tr> </thead> <tbody id="cartTable"> <?php foreach($cart_items as $item): ?> <?php $is_preorder = ($item['available'] <= 0 && $pre_order_status == 'on'); $stock_text = $is_preorder ? "<span class='text-warning fw-bold'>Pre Order</span>" : ($item['available'] > 0 ? "<span class='stock-label'>คงเหลือ <span class='remain-{$item['id']}'>{$item['available']}</span> เหรียญ</span>" : "<span class='text-danger fw-bold'>สินค้าหมด</span>"); $max_stock = $is_preorder ? 99 : $item['available']; ?> <tr data-id="<?=$item['id']?>" data-stock="<?=$item['available']?>"> <td class="text-center"> <div class="product-images"> <?php if($item['img1']): ?> <div><img src="../uploads/products/<?=$item['img1']?>" class="thumb"></div> <?php endif; ?> </div> </td> <td><?=$item['name']?></td> <td class="text-end"><?=number_format($item['price'],2)?></td> <td class="text-center"> <?=$stock_text?> <div class="qty-group mt-1"> <button type="button" class="qty-btn decrease" <?=($item['available']==0 && !$is_preorder)?'disabled':''?>>-</button> <input type="number" name="qty[<?=$item['id']?>]" value="<?=$item['qty']?>" min="1" max="<?=$max_stock?>" class="form-control form-control-sm qty-input" readonly> <button type="button" class="qty-btn increase" <?=($item['available']==0 && !$is_preorder)?'disabled':''?>>+</button> </div> </td> <td class="text-end fw-bold subtotal"><?=number_format($item['subtotal'],2)?></td> <td class="text-center"> <a href="?remove=<?=$item['id']?>" class="btn btn-sm btn-danger" onclick="return confirm('ลบเหรียญออกจากตะกร้า?')">ลบ</a> </td> </tr> <?php endforeach; ?> <tr class="table-light"> <td colspan="4" class="text-end fw-bold">รวมทั้งหมด</td> <td id="cartTotal" class="text-end fw-bold text-success fs-5"><?=number_format($total,2)?></td> <td></td> </tr> </tbody> </table> <div class="d-flex justify-content-between mb-5"> <a href="../index.php" class="btn btn-secondary">⬅ เลือกเหรียญเพิ่ม</a> </div> <div class="section-divider"></div> <?php include('checkout_form.php'); // ✅ แยกส่วน checkout ออกถ้ามี ?> </form> </div> <script> // ✅ อัปเดต session + reserved ผ่าน AJAX function updateServerCart(id, qty, callback) { fetch('update_cart.php', { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: 'id=' + id + '&qty=' + qty + '&update_reserved=1' }) .then(res => res.json()) .then(data => { if (data.error) { alert(data.error); } else if (callback) { callback(data); } }); } // ✅ ปุ่ม + / - ปรับจำนวน document.querySelectorAll('tr[data-id]').forEach(tr => { const pid = tr.getAttribute('data-id'); const input = tr.querySelector('.qty-input'); const price = parseFloat(tr.querySelector('.subtotal').textContent.replace(/,/g, '')) / parseInt(input.value); const remainEl = document.querySelector(`.remain-${pid}`); const btnMinus = tr.querySelector('.decrease'); const btnPlus = tr.querySelector('.increase'); const updateTotals = () => { let total = 0; document.querySelectorAll('.subtotal').forEach(td => { total += parseFloat(td.textContent.replace(/,/g, '')) || 0; }); document.getElementById('cartTotal').textContent = total.toLocaleString(undefined, {minimumFractionDigits:2}); }; btnMinus.addEventListener('click', () => { let val = parseInt(input.value); if (val > 1) { val--; input.value = val; tr.querySelector('.subtotal').textContent = (price * val).toLocaleString(undefined,{minimumFractionDigits:2}); updateTotals(); updateServerCart(pid, val, (data)=>{ if(remainEl) remainEl.textContent = data.remaining; }); } }); btnPlus.addEventListener('click', () => { let val = parseInt(input.value); updateServerCart(pid, val + 1, (data)=>{ if(data.error){ alert(data.error); return; } if(data.remaining !== undefined){ input.value = val + 1; tr.querySelector('.subtotal').textContent = (price * (val + 1)).toLocaleString(undefined,{minimumFractionDigits:2}); updateTotals(); remainEl.textContent = data.remaining; } }); }); }); </script> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.42 |
proxy
|
phpinfo
|
Settings