File manager - Edit - /home/webapp69.cm.in.th/u69319090039/Shop39/public/assets/app/Controllers/CheckoutController.php
Back
<?php class CheckoutController extends Controller { public function index(): void { $cart = Session::get('cart', []); if (empty($cart)) { Session::setFlash('warning', 'ตะกร้าสินค้าว่างเปล่า กรุณาเลือกสินค้าก่อนทำการสั่งซื้อ'); $this->redirect('products'); } $user = Auth::user(); $productModel = new Product(); $items = []; $total = 0; 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[] = [ 'product' => $product, 'quantity' => $cartItem['quantity'], 'price' => $price, 'subtotal' => $subtotal, 'variant_info' => $cartItem['variant_info'] ?? '' ]; } } $this->render('checkout/index', [ 'title' => 'ชำระเงินและยืนยันคำสั่งซื้อ', 'user' => $user, 'items' => $items, 'total' => $total ]); } public function process(): void { $this->validateCsrf(); $user = Auth::user(); $cart = Session::get('cart', []); if (empty($cart)) { Session::setFlash('danger', 'ไม่พบรายการสินค้าในตะกร้า'); $this->redirect('cart'); } $address = Security::sanitizeString($_POST['shipping_address'] ?? ''); if (empty($address)) { Session::setFlash('danger', 'กรุณากรอกที่อยู่จัดส่งสินค้า'); $this->redirect('checkout'); } $db = Database::getInstance(); $productModel = new Product(); // Calculate total and group items by seller $sellerGroups = []; foreach ($cart as $cartItem) { $product = $productModel->findById($cartItem['product_id']); if ($product) { $sellerId = $product['seller_id']; $price = $product['sale_price'] ?? $product['price']; $subtotal = $price * $cartItem['quantity']; $sellerGroups[$sellerId][] = [ 'product_id' => $product['id'], 'variant_info' => $cartItem['variant_info'] ?? '', 'price' => $price, 'quantity' => $cartItem['quantity'], 'subtotal' => $subtotal ]; } } // Create an order per seller $createdOrderIds = []; foreach ($sellerGroups as $sellerId => $groupItems) { $sellerTotal = array_sum(array_column($groupItems, 'subtotal')); $orderNumber = 'ORD-' . date('YmdHis') . '-' . rand(100, 999); $stmt = $db->prepare("INSERT INTO orders (order_number, user_id, seller_id, total_amount, shipping_address, status) VALUES (:num, :uid, :sid, :tot, :addr, 'paid')"); $stmt->execute([ 'num' => $orderNumber, 'uid' => $user['id'], 'sid' => $sellerId, 'tot' => $sellerTotal, 'addr' => $address ]); $orderId = (int)$db->lastInsertId(); $createdOrderIds[] = $orderId; // Insert Order Items $itemStmt = $db->prepare("INSERT INTO order_items (order_id, product_id, variant_info, price, quantity, subtotal) VALUES (:oid, :pid, :vinfo, :price, :qty, :sub)"); foreach ($groupItems as $gItem) { $itemStmt->execute([ 'oid' => $orderId, 'pid' => $gItem['product_id'], 'vinfo' => $gItem['variant_info'], 'price' => $gItem['price'], 'qty' => $gItem['quantity'], 'sub' => $gItem['subtotal'] ]); } } // Clear cart Session::remove('cart'); Session::setFlash('success', 'สร้างคำสั่งซื้อสำเร็จเรียบร้อยแล้ว!'); $this->redirect('orders'); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.71 |
proxy
|
phpinfo
|
Settings