File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/public/assets/app/Controllers/CartController.php
Back
<?php class CartController extends Controller { public function index(): void { $cart = Session::get('cart', []); $items = []; $total = 0; if (!empty($cart)) { $productModel = new Product(); foreach ($cart as $key => $cartItem) { $product = $productModel->findById($cartItem['product_id']); if ($product) { $price = $product['sale_price'] ?? $product['price']; $subtotal = $price * $cartItem['quantity']; $total += $subtotal; $items[] = [ 'key' => $key, 'product' => $product, 'quantity' => $cartItem['quantity'], 'price' => $price, 'subtotal' => $subtotal, 'variant_info' => $cartItem['variant_info'] ?? '' ]; } } } $this->render('cart/index', [ 'title' => 'ตะกร้าสินค้า', 'items' => $items, 'total' => $total ]); } public function add(): void { $this->validateCsrf(); $productId = (int)($_POST['product_id'] ?? 0); $quantity = (int)($_POST['quantity'] ?? 1); $variantId = isset($_POST['variant_id']) ? (int)$_POST['variant_id'] : 0; $productModel = new Product(); $product = $productModel->findById($productId); if (!$product) { Session::setFlash('danger', 'ไม่พบสินค้านี้ในระบบ'); $this->redirect('products'); } $cart = Session::get('cart', []); $key = $productId . '_' . $variantId; $variantInfo = 'มาตรฐาน'; if ($variantId > 0) { $db = Database::getInstance(); $stmt = $db->prepare("SELECT * FROM product_variants WHERE id = :id"); $stmt->execute(['id' => $variantId]); $var = $stmt->fetch(); if ($var) { $vovStmt = $db->prepare("SELECT pov.value, po.option_name FROM variant_option_values vov JOIN product_option_values pov ON vov.option_value_id = pov.id JOIN product_options po ON pov.option_id = po.id WHERE vov.variant_id = :vid ORDER BY po.sort_order ASC"); $vovStmt->execute(['vid' => $variantId]); $vals = $vovStmt->fetchAll(); if (!empty($vals)) { $parts = array_map(function($v) { return $v['option_name'] . ': ' . $v['value']; }, $vals); $variantInfo = implode(' / ', $parts); } } } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variant_id' => $variantId, 'variant_info' => $variantInfo, 'quantity' => $quantity ]; } Session::set('cart', $cart); Session::setFlash('success', 'เพิ่มสินค้าลงในตะกร้าเรียบร้อยแล้ว'); $this->redirect('cart'); } public function update(): void { $this->validateCsrf(); $key = $_POST['key'] ?? ''; $quantity = (int)($_POST['quantity'] ?? 1); $cart = Session::get('cart', []); if (isset($cart[$key])) { if ($quantity <= 0) { unset($cart[$key]); } else { $cart[$key]['quantity'] = $quantity; } Session::set('cart', $cart); } Session::setFlash('success', 'อัปเดตตะกร้าสินค้าแล้ว'); $this->redirect('cart'); } public function remove(): void { $this->validateCsrf(); $key = $_POST['key'] ?? ''; $cart = Session::get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); Session::set('cart', $cart); } Session::setFlash('success', 'ลบสินค้าออกจากตะกร้าแล้ว'); $this->redirect('cart'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.45 |
proxy
|
phpinfo
|
Settings