qty] public bool $showCart = false; // public bool $showCart = false; public function openCart() { $this->showCart = true; } public function closeCart() { $this->showCart = false; } public function mount() { $this->foodItems = Foods::all()->toArray(); $this->cart = session()->get('cart', []); } public function updatedCart() { session()->put('cart', $this->cart); } public function updated($name, $value) { if (Str::startsWith($name, 'cart.')) { $foodId = (int)substr($name, 5); if ($value == 0 || $value === null) { unset($this->cart[$foodId]); } else { $this->cart[$foodId] = (int)$value; } $this->updatedCart(); } } public function getCartItemsProperty() { $items = []; foreach ($this->cart as $foodId => $qty) { $food = collect($this->foodItems)->firstWhere('id', $foodId); if ($food && $qty > 0) { $items[] = [ 'id' => $foodId, 'name' => $food['name'], 'price' => $food['price'], 'image_url' => $food['image_url'], 'qty' => $qty, 'total_price' => $food['price'] * $qty, ]; } } return $items; } public function getCartTotalProperty() { return array_sum(array_map(function($item) { return $item['total_price']; }, $this->cartItems)); } public function checkout() { if (empty($this->cartItems)) { $this->dispatch('notify', message: 'Keranjang kosong'); return; } \Midtrans\Config::$serverKey = config('services.midtrans.server_key'); \Midtrans\Config::$isProduction = false; $params = [ 'transaction_details' => [ 'order_id' => 'ORDER-' . time(), 'gross_amount' => $this->cartTotal, ], 'customer_details' => [ 'first_name' => auth()->user()->name ?? 'Guest', 'email' => auth()->user()->email ?? 'guest@example.com', ], ]; try { $snapToken = \Midtrans\Snap::getSnapToken($params); $this->dispatch('midtransSnapToken', token: $snapToken); } catch (\Exception $e) { $this->dispatch('notify', message: 'Gagal memproses pembayaran: ' . $e->getMessage()); } } public function render() { return view('livewire.food-order', [ 'cartItems' => $this->cartItems, 'cartTotal' => $this->cartTotal, ])->layout('components.layouts.front'); } }