diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index fca6152c3..7ac436fbf 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -12,11 +12,11 @@ class EventServiceProvider extends ServiceProvider * * @var array */ - protected $listen = [ - 'App\Events\Event' => [ - 'App\Listeners\EventListener', - ], - ]; + // protected $listen = [ + // 'App\Events\Event' => [ + // 'App\Listeners\EventListener', + // ], + // ]; /** * Register any events for your application. diff --git a/config/app.php b/config/app.php index d7d6cb7ae..2f57f8e5c 100644 --- a/config/app.php +++ b/config/app.php @@ -241,7 +241,8 @@ return [ 'View' => Illuminate\Support\Facades\View::class, 'Datagrid' => Webkul\Ui\DataGrid\Facades\DataGrid::class, 'ProductGrid' => Webkul\Ui\DataGrid\Facades\ProductGrid::class, - 'Image' => Intervention\Image\Facades\Image::class + 'Image' => Intervention\Image\Facades\Image::class, + 'Cart' => Webkul\Cart\Facades\Cart::class, ], ]; diff --git a/packages/Webkul/Cart/src/Cart.php b/packages/Webkul/Cart/src/Cart.php new file mode 100644 index 000000000..318a81811 --- /dev/null +++ b/packages/Webkul/Cart/src/Cart.php @@ -0,0 +1,26 @@ +middleware('customer')->except(['add', 'remove']); + $this->middleware('customer')->except(['guestUnitAdd', 'guestUnitRemove']); $this->customer = $customer; @@ -45,214 +45,294 @@ class CartController extends Controller $this->cartProduct = $cartProduct; } - public function add($id) { + /** + * Function for guests + * user to add the product + * in the cart. + * + * @return Mixed + */ + public function guestUnitAdd($id) { + //this will handle the products and the cart + //when the user is not logged in + + $products = array(); + $minutes = 5; + if(Cookie::has('session_c')) { + $products = unserialize(Cookie::get('session_c')); + + foreach($products as $key => $value) { + if($value == $id) { + dump($products); + + dd('product already in the cart'); + + return redirect()->back(); + } + } + array_push($products, $id); + + Cookie::queue('session_c', serialize($products)); + + return redirect()->back(); + + } else { + array_push($products, $id); + + Cookie::queue('session_c', serialize($products), $minutes); + + // dump($products); + + return redirect()->back(); + } + } + + /** + * Function for guests + * user to remove the product + * in the cart. + * + * @return Mixed + */ + public function guestUnitRemove($id) { + + //remove the products here + if(Cookie::has('session_c')) { + $products = unserialize(Cookie::get('session_c')); + + foreach($products as $key => $value) { + if($value == $id) { + unset($products[$key]); + + array_push($products, $id); + + Cookie::queue('session_c', serialize($products)); + + return redirect()->back(); + } + } + } + } + + /*handle the after login event for the customers + when their are pruoducts in the session or cookie + of the logged in user.*/ + + public function add($id) { $products = array(); $customerLoggedIn = auth()->guard('customer')->check(); - if($customerLoggedIn) { - //customer is authenticated - - //assuming that there is data in cookie and customers' cart also. + //customer is authenticated + if ($customerLoggedIn) { + //assuming that there is data in cookie and customer's cart also. $data['customer_id'] = auth()->guard('customer')->user()->id; $data['channel_id'] = core()->getCurrentChannel()->id; - $cookieProducts = unserialize(Cookie::get('session_c')); + $customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']); - $cartCustomer = $this->cart->findOneByField('customer_id', $data['customer_id']); + //if there are products already in cart of that customer. + $customerCartId = $customerCart->id ?? $customerCart['id']; - //case when cookie has products and customer also have data in cart - if(isset($cartCustomer) && isset($cartCookie)) { - //there are already items in cart of customer + $customerCartProducts = $this->cart->withProducts($customerCartId); - //for unsetting the cookie for the cart - // Cookie::queue(Cookie::forget('session_c')); + if (isset($customerCartProducts)) { - //check if there is any repetition in the products - $cartId = $cartCustomer->id ?? $cartCustomer['id']; - - $previousCartProducts = $this->cart->withProducts($cartId); - - //if there are products already in cart of that customer - if(isset($previousCartProducts)) { - - foreach($previousCartProducts as $previousCartProduct) { - - dump($previousCartProducts); - - foreach($cookieProducts as $key => $cookieProduct) { - - if($previousCartProduct->id == $cookieProduct) { - - unset($cookieProducts[$key]); - } - } - } - } - - /*if the above block executes it will remove duplicates - else product in cookies will be stored in the database.*/ - - foreach($cookieProducts as $key => $cookieProduct) { - - $product['product_id'] = $cookieProduct; - - $product['quantity'] = 1; - - $product['cart_id'] = $cartId; - - $this->cartProduct->create($product); - } - - dump('Added the new items into the customer cart.'); - - return redirect()->back(); - - } else if(isset($cartCustomer) && !isset($cartCookie)){ - //case when there is no data in customer's cart - - $cartId = $cartCustomer->id ?? $cartCustomer['id']; - - $previousCartProducts = $this->cart->withProducts($cartId); - - foreach($previousCartProducts as $previousCartProduct) { + foreach ($customerCartProducts as $previousCartProduct) { if($previousCartProduct->id == $id) { + dd('product already exists in cart'); - dd('product already in the cart::AUTH'); + session()->flash('error', 'Product already exists in cart'); return redirect()->back(); } } + //add the product in the cart + $product['product_id'] = $id; $product['quantity'] = 1; - $product['cart_id'] = $cartId; + $product['cart_id'] = $customerCartId; $this->cartProduct->create($product); - dump('new item added in the cart'); - - return redirect()->back(); - - } else if(!isset($cartCustomer) && isset($cartCookie)) { - - $products = unserialize(Cookie::get('session_c')); - - if ($cart = $this->cart->create($data)) { - - foreach($products as $product) { - - $product['product_id'] = $id; - - $product['quantity'] = 1; - - $product['cart_id'] = $cart; - - $this->cartProduct->create($product); - } - return redirect()->back(); - - } else { - session()->flash('error', 'Cannot Add Your Items To Cart'); - - return redirect()->back(); - } - } - - } else { - //case when the customer is unauthenticated - if(Cookie::has('session_c')) { - $products = unserialize(Cookie::get('session_c')); - - foreach($products as $key => $value) { - if($value == $id) { - dd('product already in the cart'); - return redirect()->back(); - } - } - array_push($products, $id); - - Cookie::queue('session_c', serialize($products)); - - return redirect()->back(); - - } else { - array_push($products, $id); - - Cookie::queue('session_c', serialize($products), $minutes); - return redirect()->back(); } - } - // if(Cookie::has('session_id')) { - // //add the item to the cart if not already there + // //case when cookie has products and customer also have data in cart tables + // if(isset($customerCart) && isset($cartCookie)) { + // //for unsetting the cookie for the cart uncomment after all done + // // Cookie::queue(Cookie::forget('session_c')); - // //Cookie::queue(\Cookie::forget('myCookie')); - // $match_prev_session = $this->cart->findOneByField('session_id', Cookie::get('session_id')); + // //check if there is any repetition in the products + // $customerCartId = $customerCart->id ?? $customerCart['id']; - // if(isset($match_prev_session)) { + // //to check if the customer is also having some products saved in the pivot + // //for the products. + // $customerCartProducts = $this->cart->withProducts($customerCartId); - // $cart_id = $match_prev_session->id; //cart ID from the existing session values from database and cookie + // //if there are products already in cart of that customer + // if(isset($customerCartProducts)) { - // $products = $this->cart->getProducts($match_prev_session->id); //getting the products associated with that cart id. + // foreach($customerCartProducts as $previousCartProduct) { - // $existingProductsLookup = $this->cartProduct->model->where(['product_id' => $id, 'cart_id' => $cart_id]); + // dump($customerCartProducts); - // dd($existingProductsLookup); + // foreach($cookieProducts as $key => $cookieProduct) { - // //no action to be taken if the product already exists - // if(isset($existingProductsLookup)) { - // return redirect()->back(); - // } else { - // //insert the new products using cartProduct when product is new - // $cartProduct['product_id'] = $id; + // if($previousCartProduct->id == $cookieProduct) { - // $cartProduct['quantity'] = 1; - - // $cartProduct['cart_id'] = $cart_id; - - // $this->cartProduct->create($cartProduct); - - // return redirect()->back(); + // unset($cookieProducts[$key]); + // } + // } + // } // } - // } else { - // return response()->json(['Cookie Already There', Cookie::get('session_id'), $id], 200); - // } - // } else { - // /* - // make the entry in database - // and show the database entry - // in cart - // */ - // Cookie::queue('session_id', session()->getId(), $minutes); - // $data['session_id'] = session()->getId(); + // /*if the above block executes it will remove duplicates + // else product in cookies will be stored in the database.*/ - // $data['channel_id'] = core()->getCurrentChannel()->id; + // foreach($cookieProducts as $key => $cookieProduct) { - // if($cart = $this->cart->create($data)) { + // $product['product_id'] = $cookieProduct; + + // $product['quantity'] = 1; + + // $product['cart_id'] = $customerCartId; + + // $this->cartProduct->create($product); + // } + + // dump('Products in the cart synced.'); + + // return redirect()->back(); + + // } else if(isset($customerCart) && !isset($cartCookie)) { + // //case when there is no data in guest's cart + + // $customerCartId = $customerCart->id ?? $customerCart['id']; + + // $customerCartProducts = $this->cart->withProducts($customerCartId); + + // foreach($customerCartProducts as $previousCartProduct) { + + // if($previousCartProduct->id == $id) { + + // dd('product already in the cart::AUTH'); + + // return redirect()->back(); + // } + // } // $product['product_id'] = $id; // $product['quantity'] = 1; - // $product['cart_id'] = $cart; + // $product['cart_id'] = $customerCartId; // $this->cartProduct->create($product); + // dump('new item added in the cart'); + // return redirect()->back(); + + // } else if(!isset($customerCart) && isset($cartCookie)) { + + // $products = unserialize(Cookie::get('session_c')); + + // if ($cart = $this->cart->create($data)) { + + // foreach($products as $product) { + + // $product['product_id'] = $id; + + // $product['quantity'] = 1; + + // $product['cart_id'] = $cart; + + // $this->cartProduct->create($product); + // } + // return redirect()->back(); + + // } else { + // session()->flash('error', 'Cannot Add Your Items To Cart'); + + // return redirect()->back(); + // } // } // } } + /** + * use detach to remove the + * current product from cart tables + * + * @return Mixed + */ public function remove($id) { + dd("Removing Item from Cart"); } + + /** + * Function to handle merge + * and sync the cookies products + * with the existing data of cart + * in the cart tables; + */ + public function handleMerge() { + // dd(unserialize(Cookie::get('session_c'))); + if(Cookie::has('session_c')) { + $productInCookies = unserialize(Cookie::get('session_c')); + + $data['customer_id'] = auth()->guard('customer')->user()->id; + + $customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']); + + $customerCartId = $customerCart->id ?? $customerCart['id']; + + $customerCartProducts = $this->cart->withProducts($customerCartId); + + if(isset($customerCartProducts)) { + + foreach($customerCartProducts as $previousCartProduct) { + + dump($customerCartProducts); + + foreach($productInCookies as $key => $productInCookie) { + + if($previousCartProduct->id == $productInCookie) { + + unset($cookieProducts[$key]); + } + } + } + } + + /*if the above block executes it will remove duplicates + else product in cookies will be stored in the database.*/ + + foreach($cookieProducts as $key => $cookieProduct) { + + $product['product_id'] = $cookieProduct; + + $product['quantity'] = 1; + + $product['cart_id'] = $customerCartId; + + $this->cartProduct->create($product); + } + + //forget that cookie here. + + dump('Products in the cart synced.'); + + return redirect()->back(); + } + } } \ No newline at end of file diff --git a/packages/Webkul/Cart/src/Providers/CartServiceProvider.php b/packages/Webkul/Cart/src/Providers/CartServiceProvider.php index 7e5e889c3..20c8fb1a9 100644 --- a/packages/Webkul/Cart/src/Providers/CartServiceProvider.php +++ b/packages/Webkul/Cart/src/Providers/CartServiceProvider.php @@ -16,6 +16,8 @@ class CartServiceProvider extends ServiceProvider $router->aliasMiddleware('admin', RedirectIfNotAdmin::class); $router->aliasMiddleware('customer', RedirectIfNotCustomer::class); + + $this->register(EventServiceProvider::class); } /** @@ -26,5 +28,6 @@ class CartServiceProvider extends ServiceProvider public function register() { // $this->app->bind('datagrid', 'Webkul\Ui\DataGrid\DataGrid'); + $this->app->bind('Cart', 'Webkul\Cart\Cart'); } } diff --git a/packages/Webkul/Customer/src/Http/Controllers/SessionController.php b/packages/Webkul/Customer/src/Http/Controllers/SessionController.php index fe6b7aa3f..e74475b78 100644 --- a/packages/Webkul/Customer/src/Http/Controllers/SessionController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/SessionController.php @@ -4,8 +4,10 @@ namespace Webkul\Customer\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; -use Illuminate\Routing\Controller; use Webkul\Customer\Models\Customer; +use Webkul\Customer\Http\Listeners\CustomerEventsHandler; +use Illuminate\Support\Facades\Event; +use Cookie; /** * Session controller for the user customer @@ -29,6 +31,10 @@ class SessionController extends Controller $this->_config = request('_config'); + $subscriber = new CustomerEventsHandler; + + Event::subscribe($subscriber); + } public function show() @@ -53,14 +59,12 @@ class SessionController extends Controller return back(); } - $cookieProducts = unserialize(Cookie::get('session_c')); - - if(isset($cookieProducts)){ - return redirect()->action('Cart\Http\Controllers\CartController@add', [$cookieProducts]); + //Event passed to prepare cart after login + if(Cookie::has('session_c')) { + Event::fire('customer.after.login', $request->input('email')); } else { return redirect()->intended(route($this->_config['redirect'])); } - } public function destroy($id) diff --git a/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php new file mode 100644 index 000000000..cd90709f1 --- /dev/null +++ b/packages/Webkul/Customer/src/Http/Listeners/CustomerEventsHandler.php @@ -0,0 +1,48 @@ +route('cart.merge'); + // if(Cookie::has('session_c')) { + + // } + + } + + /** + * Register the listeners for the subscriber. + * + * @param Illuminate\Events\Dispatcher $events + * @return void */ + + public function subscribe($events) + { + $events->listen('customer.after.login', 'Webkul\Customer\Http\Listeners\CustomerEventsHandler@onCustomerLogin'); + } +} \ No newline at end of file diff --git a/packages/Webkul/Customer/src/Providers/EventServiceProvider.php b/packages/Webkul/Customer/src/Providers/EventServiceProvider.php index b322a94b0..ebf899ea7 100644 --- a/packages/Webkul/Customer/src/Providers/EventServiceProvider.php +++ b/packages/Webkul/Customer/src/Providers/EventServiceProvider.php @@ -2,13 +2,14 @@ namespace Webkul\Customer\Providers; -use Illuminate\Support\ServiceProvider; +use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\View; use Webkul\Customer\Menu; class EventServiceProvider extends ServiceProvider { + /** * Bootstrap services. * @@ -27,6 +28,7 @@ class EventServiceProvider extends ServiceProvider public function createCustomerAccountSideMenu() { + Event::listen('customer.menu.create', function () { return Menu::create(function ($menu) { Event::fire('customer.menu.build', $menu); diff --git a/packages/Webkul/Shop/src/Http/routes.php b/packages/Webkul/Shop/src/Http/routes.php index 4885ec023..bc525dd9d 100644 --- a/packages/Webkul/Shop/src/Http/routes.php +++ b/packages/Webkul/Shop/src/Http/routes.php @@ -15,11 +15,17 @@ Route::group(['middleware' => ['web']], function () { ])->name('shop.products.index'); // //Routes for product cart - // Route::post('products/cart/test', 'Webkul\Cart\Http\Controllers\CartController@test')->name('cart.test'); - Route::post('products/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@add')->name('cart.add'); + Route::post('products/guest/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@guestUnitAdd')->name('cart.guest.add'); + + Route::post('product/guest/cart/remove/{id}', 'Webkul\Cart\Http\Controllers\CartController@guestUnitRemove')->name('cart.guest.remove'); + + Route::post('product/customer/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@add')->name('cart.customer.add'); + + Route::post('product/customer/cart/remove/{id}', 'Webkul\Cart\Http\Controllers\CartController@remove')->name('cart.customer.remove'); + + Route::get('product/customer/cart/merge', 'Webkul\Cart\Http\Controllers\CartController@handleMerge')->name('cart.merge'); - Route::post('product/cart/remove/{id}', 'Webkul\Cart\Http\Controllers\CartController@remove')->name('cart.remove'); //Routes for product cart ends diff --git a/packages/Webkul/Shop/src/Resources/views/products/view/gallery.blade.php b/packages/Webkul/Shop/src/Resources/views/products/view/gallery.blade.php index 0bd7234fb..3e478575e 100644 --- a/packages/Webkul/Shop/src/Resources/views/products/view/gallery.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/products/view/gallery.blade.php @@ -17,7 +17,7 @@
-
+ {{ csrf_field() }} @@ -31,5 +31,6 @@
--}} + {{-- {{ dd(unserialize(Cookie::get('session_c'))) }} --}}