Cart Module in progress 70%

This commit is contained in:
prashant-webkul 2018-09-11 16:49:40 +05:30
parent 40e04abddf
commit 398caff7bc
11 changed files with 359 additions and 169 deletions

View File

@ -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.

View File

@ -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,
],
];

View File

@ -0,0 +1,26 @@
<?php
namespace Webkul\Cart;
use Carbon\Carbon;
use Webkul\Cart\Models\Cart as Cartmodel;
use Webkul\Cart\Models\CartProduct;
use Webkul\Core\Models\Channel as ChannelModel;
use Webkul\Core\Models\Locale as LocaleModel;
use Webkul\Core\Models\TaxCategory as TaxCategory;
use Webkul\Core\Models\TaxRate as TaxRate;
use Webkul\Customer\Models\Customer;
use Cookie;
class Cart {
public function getCart($id) {
dd('juice', $id);
}
public function getProducts($id) {
dd('juice 1', $id);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Webkul\Cart\Facades;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
class Cart extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'cart';
}
}

View File

@ -4,10 +4,10 @@ namespace Webkul\Cart\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
// use Illuminate\Routing\Controller;
use Webkul\Cart\Repositories\CartRepository as Cart;
use Webkul\Cart\Repositories\CartProductRepository as cartProduct;
use Webkul\Customer\Repositories\CustomerRepository;
use Session;
use Cookie;
/**
@ -36,7 +36,7 @@ class CartController extends Controller
protected $customer;
public function __construct(Cart $cart, cartProduct $cartProduct, CustomerRepository $customer) {
$this->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();
}
}
}

View File

@ -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');
}
}

View File

@ -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)

View File

@ -0,0 +1,48 @@
<?php
namespace Webkul\Customer\Http\Listeners;
use Cookie;
use Cart;
class CustomerEventsHandler {
/**
* Handle Customer login events.
*/
public function onCustomerLogin($event)
{
/**
* handle the user login
* event to manage the
* after login, if
* the user has added any
* products as guest then
* the cart items from session
* will be transferred from
* cookie to the cart table
* in the database.
*
* Check whether cookie is
* present or not and then
* check emptiness and then
* do the appropriate actions.
*/
Cart::getCart(1);
// return redirect()->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');
}
}

View File

@ -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);

View File

@ -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

View File

@ -17,7 +17,7 @@
</div>
<div class="product-button-group">
<form method="POST" action="{{ route('cart.add', $product->id) }}">
<form method="POST" @auth('customer') action="{{ route('cart.customer.add', $product->id) }}" @endauth @guest action="{{ route('cart.guest.add', $product->id) }}" @endguest>
{{ csrf_field() }}
<input type="hidden" name="product_id" value="{{ $product->id }}">
@ -31,5 +31,6 @@
<input type="hidden" name="qty" value="1">
<button type="submit" class="btn btn-lg btn-primary buy-now">Buy Now</button>
</form> --}}
{{-- {{ dd(unserialize(Cookie::get('session_c'))) }} --}}
</div>
</div>