diff --git a/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php b/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php index 01b6b0656..535293e54 100644 --- a/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php +++ b/packages/Webkul/API/Http/Controllers/Customer/CustomerController.php @@ -73,8 +73,6 @@ class CustomerController extends Controller } } - dd($data); - $result = $this->customer->update($data); if($result) { @@ -83,4 +81,34 @@ class CustomerController extends Controller return response()->json(false, 200); } } + + /** + * Get all instances of cart for currently logged in customer + * + * @return collection Cart + */ + public function getAllCart() { + $carts = $this->customer->carts; + + if($cart->count() > 0) { + return response()->json(['message' => 'successful','items' => $cart], 200); + } else { + return response()->json(['message' => 'empty', 'items' => null], 200); + } + } + + public function getActiveCart() { + $cart = Cart::getCart(); + + if($cart == null) { + return response()->json(['message' => 'empty', 'items' => 'null']); + } + + if($cart->count() > 0 ) { + return response()->json(['message' => 'success', 'items' => $cart]); + } else { + return response()->json(['message' => 'empty', 'items' => 'null']); + } + } + } \ No newline at end of file diff --git a/packages/Webkul/API/Http/Controllers/Shop/CartController.php b/packages/Webkul/API/Http/Controllers/Shop/CartController.php index b27298b33..81fec6b8b 100644 --- a/packages/Webkul/API/Http/Controllers/Shop/CartController.php +++ b/packages/Webkul/API/Http/Controllers/Shop/CartController.php @@ -8,6 +8,7 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\Event; use Webkul\Checkout\Repositories\CartRepository; use Auth; +use Cart; /** * Cart controller for the APIs of User Cart @@ -24,37 +25,81 @@ class CartController extends Controller public function __construct(CartRepository $cart) { $this->cart = $cart; - - if(auth()->guard('customer')->check()) { - $this->customer = auth()->guard('customer')->user(); - } else { - $this->customer['message'] = 'unauthorized'; - $this->unAuthorized(); - } - } - - public function unAuthorized() { - return response()->json($this->customer, 401); - } - - public function getAllCart() { - $carts = $this->customer->carts; - - if($cart->count() > 0) { - return response()->json($cart, 200); - } else { - return response()->json('Cart Empty', 200); - } - } - - public function getActiveCart() { - return $this->customer->cart; } /** - * Add a new item in the cart + * Function to get the current cart instance for customer or guest + * + * @return Response array && Collection Cart */ - public function add($id) { - dd('add to cart', $id); + public function get() { + $cart = Cart::getCart(); + + if($cart->count() > 0) { + return response()->json(['message' => 'success', 'items' => $cart]); + } else { + return response()->json(['message' => 'empty', 'items' => null]); + } + } + + /** + * Function for guests user to add the product in the cart. + * + * @return Mixed + */ + public function add($id) + { + $result = Cart::add($id, request()->all()); + + if($result) { + Cart::collectTotals(); + + return response()->json(['message' => 'successful', 'items' => Cart::getCart()->items]); + } else { + return response()->json(['message' => 'failed', 'items' => Cart::getCart()->items]); + } + } + + /** + * Removes the item from the cart if it exists + * + * @param integer $itemId + */ + public function remove($itemId) { + $result = Cart::removeItem($itemId); + + Cart::collectTotals(); + + return response()->json(['message' => $result, 'items' => Cart::getCart()]); + } + + /** + * Updates the quantity of the items present in the cart. + * + * @return response JSON + */ + public function updateBeforeCheckout() { + $request = request()->except('_token'); + + foreach($request['qty'] as $id => $quantity) { + if($quantity <= 0) { + return response()->json(['message' => 'Illegal Quantity', 'status' => 'error']); + } + } + + foreach($request['qty'] as $key => $value) { + $item = $this->cartItem->findOneByField('id', $key); + + $data['quantity'] = $value; + + Cart::updateItem($item->product_id, $data, $key); + + unset($item); + unset($data); + } + + Cart::collectTotals(); + + return response()->json(['message' => 'success']); } } \ No newline at end of file diff --git a/packages/Webkul/API/Http/api.php b/packages/Webkul/API/Http/api.php index dcf4af342..d7ac91159 100644 --- a/packages/Webkul/API/Http/api.php +++ b/packages/Webkul/API/Http/api.php @@ -34,19 +34,18 @@ Route::group(['namespace' => 'Webkul\API\Http\Controllers\Customer', 'prefix' => //cart //active + inactive instances of cart for the current logged in user - Route::get('get/all', 'CartController@getAllCart'); + Route::get('get/all', 'CustomerController@getAllCart'); //active instances of cart for the current logged in user - Route::get('get/active', 'CartController@getActiveCart'); - //inactive instances of cart for the current logged in user - Route::get('get/inactive', 'CartController@getInactiveCart'); + Route::get('get/active', 'CustomerController@getActiveCart'); }); Route::group(['namespace' => 'Webkul\API\Http\Controllers\Shop', 'prefix' => 'api/cart'], function ($router) { //cart + Route::get('get', 'CartController@get'); //add item in the cart - // Route::get('add/{id}', 'CartController@add'); + Route::post('add/{id}', 'CartController@add'); //remove item to the cart - // Route::get('remove/{id}', 'CartController@add'); + Route::get('remove/{id}', 'CartController@remove'); }); Route::group(['namespace' => 'Webkul\API\Http\Controllers\Product', 'prefix' => 'api/product'], function ($router) {