cartRepo = $cartRepository; $this->productRepo = $productRepository; $this->courierRepo = $courierRepository; $this->productAttributeRepo = $productAttributeRepository; } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $courier = $this->courierRepo->findCourierById(request()->session()->get('courierId', 1)); $shippingFee = $this->cartRepo->getShippingFee($courier); return view('front.carts.cart', [ 'cartItems' => $this->cartRepo->getCartItemsTransformed(), 'subtotal' => $this->cartRepo->getSubTotal(), 'tax' => $this->cartRepo->getTax(), 'shippingFee' => $shippingFee, 'total' => $this->cartRepo->getTotal(2, $shippingFee) ]); } /** * Store a newly created resource in storage. * * @param AddToCartRequest $request * @return \Illuminate\Http\Response */ public function store(AddToCartRequest $request) { $product = $this->productRepo->findProductById($request->input('product')); if ($product->attributes()->count() > 0) { $productAttr = $product->attributes()->where('default', 1)->first(); if (isset($productAttr->sale_price)) { $product->price = $productAttr->price; if (!is_null($productAttr->sale_price)) { $product->price = $productAttr->sale_price; } } } $options = []; if ($request->has('productAttribute')) { $attr = $this->productAttributeRepo->findProductAttributeById($request->input('productAttribute')); $product->price = $attr->price; $options['product_attribute_id'] = $request->input('productAttribute'); $options['combination'] = $attr->attributesValues->toArray(); } $this->cartRepo->addToCart($product, $request->input('quantity'), $options); return redirect()->route('cart.index') ->with('message', 'Add to cart successful'); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(UpdateCartRequest $request, $id) { $this->cartRepo->updateQuantityInCart($id, $request->input('quantity')); request()->session()->flash('message', 'Update cart successful'); return redirect()->route('cart.index'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $this->cartRepo->removeToCart($id); request()->session()->flash('message', 'Removed to cart successful'); return redirect()->route('cart.index'); } }