private function getTotalByCart($cartId) { $cartItems = CartItem::where('cart_id', $cartId)->get(); $total = 0; foreach ($cartItems as $item) { $unitPrice = $item['unit_price']; $quantity = $item['quantity']; $productDiscount = $item['discount_percentage'] / 100; $senior = $item['senior_citizen']; if ($productDiscount) { $discountAmount = $total * $productDiscount; $total += $total - $discountAmount; } else { if ($senior) { $discountAmount = $total * $productDiscount; $vat = $unitPrice / 1.12 * 0.12; $unitPrice = $unitPrice - $vat; $tmpTotal = $unitPrice * $quantity; $total += $tmpTotal - $discountAmount; } else { $total += $unitPrice * $quantity; } } } return $total; }
public function new($product_id, Request $request) { // get or create cart if (!Session::get('cart_id')) { $cart = CartsController::new(); $cart_id = $cart->id; Session::put('cart_id', $cart_id); } else { $cart_id = Session::get('cart_id'); } //get product data for cart item $product = Product::whereId($product_id)->firstOrFail(); $cartitem = new CartItem(['cart_id' => $cart_id, 'product_id' => $product->id, 'qty' => $request->get('qty'), 'price_ea' => $product->price, 'price_total' => $product->price * $request->get('qty')]); $cartitem->save(); $request->session()->flash('status', 'Item added to cart!'); return redirect('/'); }
/** * Run the database seeds. * * @return void */ public function run() { for ($i = 1; $i < 10; $i++) { \App\Models\Cart::create(['table_id' => $i, 'status' => 'active']); } $faker = Faker\Factory::create(); for ($i = 1; $i < 10; $i++) { for ($k = 1; $k < 6; $k++) { \App\Models\CartItem::create(['cart_id' => $i, 'product_id' => $k, 'quantity' => $faker->numberBetween(5, 20), 'status' => 'active']); } } }
public function changeQuantity(Request $request) { // verify parameters if (!$request->has("itemId") || !$request->has("quantity")) { return new Response("Request must contain itemId and quantity", Response::HTTP_BAD_REQUEST); } $itemId = $request->input("itemId"); $quantity = $request->input("quantity"); // verify valid itemId $cartItem = CartItem::find($itemId); if (is_null($cartItem)) { return new Response("Item id is invalid: " . $itemId, Response::HTTP_NOT_FOUND); } $cartItem->quantity = $quantity; $cartItem->save(); return $this->success(array('itemId' => $itemId, 'quantity' => $quantity)); }
public function removeItemFromCart($cartId, $productId) { \Log::info("Deleting cartId " . $cartId . " with product " . $productId); $item = CartItem::where('cart_id', $cartId)->where('product_id', $productId)->forceDelete(); return $this->responseOk($item); }
public function completeOrder($cartItemId) { $item = CartItem::where('id', $cartItemId)->update(['status' => 'completed']); $items = CartItem::with('product', 'cart.table')->where('status', 'pending')->get(); return $this->responseOk($this->parseItems($items->toArray())); }