File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/CartController.php
Back
<?php /** * CartController * Handles shopping cart viewing, item additions, variation options, quantity modifications, and selection toggling */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/CartService.php'; require_once __DIR__ . '/../Helpers/Auth.php'; require_once __DIR__ . '/../Helpers/Session.php'; require_once __DIR__ . '/../Helpers/Url.php'; class CartController extends Controller { private CartService $cartService; public function __construct() { $this->cartService = new CartService(); } public function index(): void { $userId = Auth::id(); $cartData = $this->cartService->getCart($userId); $this->render('cart/index', [ 'cart' => $cartData, 'pageTitle' => __('cart') . ' - CARGOO' ], 'main'); } public function add(): void { $userId = Auth::id(); $productId = (int)($_POST['product_id'] ?? 0); $quantity = max(1, (int)($_POST['quantity'] ?? 1)); $variationSummary = trim($_POST['variation_summary'] ?? ''); $unitPrice = !empty($_POST['unit_price']) ? (float)$_POST['unit_price'] : null; $skuId = !empty($_POST['sku_id']) ? (int)$_POST['sku_id'] : null; $result = $this->cartService->addItem($userId, $productId, $quantity, $variationSummary, $unitPrice, $skuId); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json($result); } if (!$result['success']) { Session::setFlash('danger', $result['message']); } else { Session::setFlash('success', $result['message']); } if (!empty($_POST['buy_now'])) { $this->redirect('/checkout'); } $this->redirect('/cart'); } public function update(): void { $userId = Auth::id(); $itemId = (int)($_POST['cart_item_id'] ?? $_POST['product_id'] ?? 0); $quantity = (int)($_POST['quantity'] ?? 1); $result = $this->cartService->updateQuantity($userId, $itemId, $quantity); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json($result); } $this->redirect('/cart'); } public function remove(): void { $userId = Auth::id(); $itemId = (int)($_POST['cart_item_id'] ?? $_POST['product_id'] ?? 0); $result = $this->cartService->removeItem($userId, $itemId); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json($result); } Session::setFlash('success', $result['message']); $this->redirect('/cart'); } public function toggleSelect(): void { $userId = Auth::id(); $itemId = (int)($_POST['cart_item_id'] ?? $_POST['product_id'] ?? 0); $isSelected = !empty($_POST['is_selected']); $result = $this->cartService->toggleSelect($userId, $itemId, $isSelected); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json($result); } $this->redirect('/cart'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings