middleware(CheckItemStock::class)->only(['store', 'update']); } /** * Store a newly created resource in storage. * * @param StoreCartItemRequest $request * * @return \Modules\Cart\Cart */ public function store(StoreCartItemRequest $request) { Cart::store( $request->product_id, $request->variant_id, $request->qty, $request->options ?? [] ); return Cart::instance(); } /** * Update the specified resource in storage. * * @param UpdateCartItemRequest $request * @param string $cartItemId * * @return \Modules\Cart\Cart */ public function update(UpdateCartItemRequest $request, string $cartItemId) { Cart::updateQuantity($cartItemId, request('qty')); $cartWithCoupon = null; $couponCode = request()->query('coupon_code'); if ($couponCode) { $coupon = Coupon::findByCode($couponCode); try { resolve(Pipeline::class) ->send($coupon) ->through($this->checkers) ->then(function ($coupon) use (&$cartWithCoupon) { Cart::applyCoupon($coupon); $cartWithCoupon = json_encode(Cart::instance()); Cart::removeCoupon(); }); } catch (Exception) { //Just suppressing the exception } } return $cartWithCoupon ?? Cart::instance(); } /** * Remove the specified resource from storage. * * @param string $cartItemId * * @return \Modules\Cart\Cart */ public function destroy(string $cartItemId) { Cart::remove($cartItemId); $cartWithCoupon = null; $couponCode = request()->query('coupon_code'); if ($couponCode) { $coupon = Coupon::findByCode($couponCode); try { resolve(Pipeline::class) ->send($coupon) ->through($this->checkers) ->then(function ($coupon) use (&$cartWithCoupon) { Cart::applyCoupon($coupon); $cartWithCoupon = json_encode(Cart::instance()); Cart::removeCoupon(); }); } catch (Exception) { //Just suppressing the exception } } return $cartWithCoupon ?? Cart::instance(); } }