public function addCart(Request $request) { $book = Book::find($request->id); if ($request->quantity <= $book->quantity) { $cart_old = Cart::where('book_id', '=', $request->id)->where(function ($query) use($request) { $query->where('user_id', '=', Auth::check() ? Auth::user()->id : 0)->orWhere('remember_token', '=', $request->header('X-CSRF-TOKEN')); }); if ($cart_old->count() > 0) { $check = $cart_old->firstOrFail()->update(['quantity' => $cart_old->firstOrFail()->quantity + $request->quantity]); } else { $cart = new Cart(); $cart->user_id = Auth::check() ? Auth::user()->id : null; $cart->book_id = $request->id; $cart->quantity = $request->quantity; $cart->remember_token = $request->header('X-CSRF-TOKEN'); $check = $cart->save(); } if ($check) { return "true"; } else { return "Lỗi thêm hàng vào giỏ. Vui lòng thử lại!"; } } else { return "Quá số hàng trong kho. Vui lòng thử lại!"; } }
/** * @return Cart */ protected function getCart() { $cart = Cart::where('user_id', Auth::user()->id)->first(); if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); return $cart; } return $cart; }
protected function _createCart($force = false) { $user = Sentinel::getUser(); $cart = Cart::where('user_id', $user->id)->where('closed', '!=', 1)->orderBy('id', 'DESC')->first(); if (!$cart) { if ($force == true) { return null; } $cart = new Cart(); $cart->user_id = $user->id; $cart->save(); } return $cart; }
public function showCart() { $cart = Cart::where('user_id', Auth::user()->id)->first(); if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); } $items = $cart->cartItems; $total = 0; foreach ($items as $item) { $total += $item->product->price; } return view('cart.view', ['items' => $items, 'total' => $total]); }
public function showCart() { // displays current items a user currently has in their cart $cart = Cart::where('user_id', Auth::user()->id)->first(); // A new cart is generated if a user has not got one, but accesses the showCart url. if (!$cart) { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->save(); } // Runs a loop that infers for every item bought, the total value will be increased by that price. $items = $cart->cartItems; $total = 0; foreach ($items as $item) { $total += $item->movie->price; } return view('cart.show', ['items' => $items, 'total' => $total]); }
public function add($product_id, Request $request) { $pid = Product::findOrFail($product_id)->id; if (Product::find($pid)->quantity - $request->qty < 0) { return Redirect()->back()->with(['flash_message' => 'Out of Stock', 'flash-warning' => true]); } /** * Check If the user is a guest , if so store the cart in a session */ if (Auth::guest()) { $exists = 0; /** * Check if the product already exists in the cart , if so increment the quantity */ if (Session::has('cart')) { foreach (Session::get('cart') as $key => $cart) { if ($cart['product_id'] == $product_id) { $cart['qty'] += $request->qty; if ($cart['qty'] <= 0) { $cart['qty'] = 1; } Session::forget('cart.' . $key); if ($request->options) { Session::push('cart', ['product_id' => $product_id, 'qty' => $cart['qty'], 'options' => implode(',', $request->options)]); } else { Session::push('cart', ['product_id' => $product_id, 'qty' => $cart['qty']]); } $exists = 1; break; } } } /** * If the product is not in the cart , add a new one */ if (!$exists) { if ($request->options) { Session::push('cart', ['product_id' => $product_id, 'qty' => $request->qty, 'options' => implode(',', $request->options)]); } else { Session::push('cart', ['product_id' => $product_id, 'qty' => $request->qty]); } } } else { if (count($cart = Cart::whereProduct_idAndUser_id($product_id, Auth::user()->id)->first())) { $request->qty ? $cart->amount += $request->qty : ($cart->amount += 1); if ($cart->amount <= 0) { $cart->amount = 1; } $cart->save(); } else { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->product_id = $pid; if ($request->options) { $cart->options = implode(',', $request->options); } $request->qty ? $cart->amount = $request->qty : ($cart->amount = 1); if ($cart->amount <= 0) { $cart->amount = 1; } $cart->save(); } } return \Redirect()->back()->with(['flash_message' => 'Added to Cart !']); }
public function moveCartToDB() { if (Session::has('cart')) { foreach (Session::get('cart') as $item) { if (count($cart = Cart::whereProduct_idAndUser_id($item['product_id'], Auth::user()->id)->first())) { $cart->amount += $item['qty']; $cart->save(); } else { $cart = new Cart(); $cart->user_id = Auth::user()->id; $cart->product_id = $item['product_id']; $cart->amount = $item['qty']; if (isset($item['options'])) { $cart->options = $item['options']; } $cart->save(); } } } }