/** * Get the cart information * * @param $cartId * @return \Symfony\Component\HttpFoundation\Response */ public function getCart($tableId) { $cart = Cart::with('items.product.category')->where('table_id', $tableId)->Where(function ($q) { $q->where('status', Cart::PENDING)->orWhere('status', Cart::CONFIRMED); })->first(); if (!$cart) { return $this->responseNotFound(['There is no cart with that table id']); } $cart = $this->parseCart($cart); return response()->json($cart); }
private function total_price() { $total_price = 0; $carts = Cart::with('good')->where('user_id', $this->user->id)->get(); foreach ($carts as $cart) { $total_price += $cart->number * $cart->good->price; } return $total_price; }
/** * 结算,生成订单,删除购物车 */ public function setorder() { $address = Address::where('user_id', $this->user->id)->first(); if ($address) { $carts = Cart::with('good')->where('user_id', $this->user->id)->get(); $order = new Order(); $order->user_id = $this->user->id; $order->status = 0; $order->address_id = $address->id; $order->express_id = 1; $order->express_code = 918682605098; $order->total_price = $this->total_price(); $order->save(); foreach ($carts as $cart) { $order_good = new Order_good(); $order_good->good_id = $cart->good_id; $order_good->order_id = $order->id; $order_good->number = $cart->num; $order_good->save(); } $carts = Cart::where('user_id', $this->user->id)->delete(); $order_goods = Order_good::with('good')->where('order_id', $order->id)->get(); return view('wechat.setorder', ['address' => $address, 'carts' => $order_goods, 'total_price' => $order->total_price, 'cart_number' => 0, 'user_id' => $this->user->id]); } else { return redirect(url('/address', $this->user->id)); } }