Cart Module being modified accodrding to session
This commit is contained in:
parent
4e7b1f157b
commit
ac093c465b
|
|
@ -33,12 +33,12 @@
|
|||
"webkul/laravel-ui": "self.version",
|
||||
"webkul/laravel-core": "self.version",
|
||||
"webkul/laravel-attribute": "self.version",
|
||||
"webkul/laravel-cart": "self.version",
|
||||
"webkul/laravel-customer": "self.version",
|
||||
"webkul/laravel-category": "self.version",
|
||||
"webkul/laravel-product": "self.version",
|
||||
"webkul/laravel-shop": "self.version",
|
||||
"webkul/laravel-theme": "self.version",
|
||||
"webkul/laravel-cart": "self.version"
|
||||
"webkul/laravel-theme": "self.version"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
|
|
@ -51,14 +51,14 @@
|
|||
"Webkul\\Admin\\": "packages/Webkul/Admin/src",
|
||||
"Webkul\\Ui\\": "packages/Webkul/Ui/src",
|
||||
"Webkul\\Category\\": "packages/Webkul/Category/src",
|
||||
"Webkul\\Cart\\": "packages/Webkul/Cart/src",
|
||||
"Webkul\\Attribute\\": "packages/Webkul/Attribute/src",
|
||||
"Webkul\\Shop\\": "packages/Webkul/Shop/src",
|
||||
"Webkul\\Core\\": "packages/Webkul/Core/src",
|
||||
"Webkul\\Customer\\": "packages/Webkul/Customer/src",
|
||||
"Webkul\\Inventory\\": "packages/Webkul/Inventory/src",
|
||||
"Webkul\\Product\\": "packages/Webkul/Product/src",
|
||||
"Webkul\\Theme\\": "packages/Webkul/Theme/src",
|
||||
"Webkul\\Cart\\": "packages/Webkul/Cart/src"
|
||||
"Webkul\\Theme\\": "packages/Webkul/Theme/src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ return [
|
|||
Webkul\User\Providers\UserServiceProvider::class,
|
||||
Webkul\Admin\Providers\AdminServiceProvider::class,
|
||||
Webkul\Ui\Providers\UiServiceProvider::class,
|
||||
Webkul\Category\Providers\CategoryServiceProvider::class,
|
||||
// Webkul\Category\Providers\CategoryServiceProvider::class,
|
||||
Webkul\Attribute\Providers\AttributeServiceProvider::class,
|
||||
Webkul\Core\Providers\CoreServiceProvider::class,
|
||||
Webkul\Shop\Providers\ShopServiceProvider::class,
|
||||
|
|
|
|||
|
|
@ -3,13 +3,11 @@
|
|||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Prashant",
|
||||
"email": "prashant@webkul.com"
|
||||
"name": "Prashant Singh",
|
||||
"email": "prashant.singh852@webkul.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"webkul/laravel-core": "dev-master"
|
||||
},
|
||||
"require": {},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Webkul\\Cart\\": "src/"
|
||||
|
|
@ -18,9 +16,8 @@
|
|||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Webkul\\Cart\\Providers\\CartServiceProvider"
|
||||
],
|
||||
"aliases": {}
|
||||
"Webkul\\Cart\\CartServiceProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
|
|
|
|||
|
|
@ -4,23 +4,344 @@ namespace Webkul\Cart;
|
|||
|
||||
use Carbon\Carbon;
|
||||
|
||||
use Webkul\Cart\Models\Cart as Cartmodel;
|
||||
use Webkul\Cart\Models\CartProduct;
|
||||
//Cart repositories
|
||||
use Webkul\Cart\Repositories\CartRepository;
|
||||
use Webkul\Cart\Repositories\CartProductRepository;
|
||||
|
||||
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;
|
||||
//Customer repositories
|
||||
use Webkul\Customer\Repositories\CustomerRepository;
|
||||
|
||||
use Webkul\Customer\Models\Customer;
|
||||
use Cookie;
|
||||
|
||||
/**
|
||||
* Cart facade for all
|
||||
* the methods to be
|
||||
* implemented for Cart.
|
||||
*
|
||||
* @author Prashant Singh <prashant.singh852@webkul.com>
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
*/
|
||||
|
||||
class Cart {
|
||||
public function getCart($id) {
|
||||
dd('juice', $id);
|
||||
|
||||
protected $_config;
|
||||
|
||||
protected $cart;
|
||||
|
||||
protected $cartProduct;
|
||||
|
||||
protected $customer;
|
||||
|
||||
public function __construct(CartRepository $cart, CartProductRepository $cartProduct, CustomerRepository $customer) {
|
||||
|
||||
$this->customer = $customer;
|
||||
|
||||
$this->cart = $cart;
|
||||
|
||||
$this->cartProduct = $cartProduct;
|
||||
}
|
||||
|
||||
public function getProducts($id) {
|
||||
dd('juice 1', $id);
|
||||
public function guestUnitAdd($id) {
|
||||
|
||||
$products = array();
|
||||
|
||||
$minutes = 10;
|
||||
|
||||
// if(Session::get('current_session_id'))
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
|
||||
// //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;
|
||||
|
||||
$customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']);
|
||||
|
||||
//if there are products already in cart of that customer.
|
||||
$customerCartId = $customerCart->id ?? $customerCart['id'];
|
||||
|
||||
$customerCartProducts = $this->cart->getProducts($customerCartId);
|
||||
|
||||
if (isset($customerCartProducts)) {
|
||||
|
||||
foreach ($customerCartProducts as $previousCartProduct) {
|
||||
|
||||
if($previousCartProduct->id == $id) {
|
||||
dd('product already exists in cart');
|
||||
|
||||
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'] = $customerCartId;
|
||||
|
||||
$this->cartProduct->create($product);
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
// }
|
||||
|
||||
// //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'));
|
||||
|
||||
// //check if there is any repetition in the products
|
||||
// $customerCartId = $customerCart->id ?? $customerCart['id'];
|
||||
|
||||
// //to check if the customer is also having some products saved in the pivot
|
||||
// //for the products.
|
||||
// $customerCartProducts = $this->cart->getProducts($customerCartId);
|
||||
|
||||
// //if there are products already in cart of that customer
|
||||
// if(isset($customerCartProducts)) {
|
||||
|
||||
// foreach($customerCartProducts as $previousCartProduct) {
|
||||
|
||||
// dump($customerCartProducts);
|
||||
|
||||
// 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'] = $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->getProducts($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'] = $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() {
|
||||
|
||||
$productsInCookie = unserialize(Cookie::get('session_c'));
|
||||
|
||||
$data['customer_id'] = auth()->guard('customer')->user()->id;
|
||||
|
||||
$data['channel_id'] = core()->getCurrentChannel()->id;
|
||||
|
||||
$customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']);
|
||||
|
||||
if(isset($customerCart)) {
|
||||
|
||||
$customerCartId = $customerCart->id ?? $customerCart['id'];
|
||||
|
||||
$customerCartProducts = $this->cart->getProducts($customerCartId);
|
||||
|
||||
if(isset($customerCartProducts)) {
|
||||
|
||||
foreach($customerCartProducts as $previousCartProduct) {
|
||||
|
||||
foreach($productsInCookie as $key => $productInCookie) {
|
||||
|
||||
if($previousCartProduct->id == $productInCookie) {
|
||||
|
||||
unset($productsInCookie[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*if the above block executes it will remove duplicates
|
||||
else product in cookies will be stored in the database.*/
|
||||
|
||||
foreach($productsInCookie as $key => $cookieProduct) {
|
||||
|
||||
$product['product_id'] = $cookieProduct;
|
||||
|
||||
$product['quantity'] = 1;
|
||||
|
||||
$product['cart_id'] = $customerCartId;
|
||||
|
||||
$this->cartProduct->create($product);
|
||||
}
|
||||
|
||||
//forget that cookie here.
|
||||
Cookie::queue(Cookie::forget('session_c'));
|
||||
} else {
|
||||
|
||||
if($cart = $this->cart->create($data)) {
|
||||
|
||||
foreach($productsInCookie as $productInCookie) {
|
||||
|
||||
$product['product_id'] = $cookieProduct;
|
||||
|
||||
$product['quantity'] = 1;
|
||||
|
||||
$product['cart_id'] = $cart->id;
|
||||
|
||||
$this->cartProduct->create($product);
|
||||
}
|
||||
}
|
||||
//forget the Cookie
|
||||
Cookie::queue(Cookie::forget('session_c'));
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Webkul\Cart\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
class Cart extends Facade
|
||||
|
|
|
|||
|
|
@ -4,10 +4,15 @@ 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;
|
||||
|
||||
//Cart repositories
|
||||
use Webkul\Cart\Repositories\CartRepository;
|
||||
use Webkul\Cart\Repositories\CartProductRepository;
|
||||
|
||||
//Customer repositories
|
||||
use Webkul\Customer\Repositories\CustomerRepository;
|
||||
|
||||
use Cart;
|
||||
use Cookie;
|
||||
|
||||
/**
|
||||
|
|
@ -35,8 +40,9 @@ class CartController extends Controller
|
|||
|
||||
protected $customer;
|
||||
|
||||
public function __construct(Cart $cart, cartProduct $cartProduct, CustomerRepository $customer) {
|
||||
$this->middleware('customer')->except(['guestUnitAdd', 'guestUnitRemove']);
|
||||
public function __construct(CartRepository $cart, CartProductRepository $cartProduct, CustomerRepository $customer) {
|
||||
|
||||
$this->middleware('customer')->except(['add', 'remove']);
|
||||
|
||||
$this->customer = $customer;
|
||||
|
||||
|
|
@ -52,287 +58,22 @@ class CartController extends Controller
|
|||
*
|
||||
* @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();
|
||||
|
||||
//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;
|
||||
|
||||
$customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']);
|
||||
|
||||
//if there are products already in cart of that customer.
|
||||
$customerCartId = $customerCart->id ?? $customerCart['id'];
|
||||
|
||||
$customerCartProducts = $this->cart->withProducts($customerCartId);
|
||||
|
||||
if (isset($customerCartProducts)) {
|
||||
|
||||
foreach ($customerCartProducts as $previousCartProduct) {
|
||||
|
||||
if($previousCartProduct->id == $id) {
|
||||
dd('product already exists in cart');
|
||||
|
||||
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'] = $customerCartId;
|
||||
|
||||
$this->cartProduct->create($product);
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
if(auth()->guard('customer')->check()) {
|
||||
Cart::add($id);
|
||||
} else {
|
||||
Cart::guestUnitAdd($id);
|
||||
}
|
||||
|
||||
// //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'));
|
||||
|
||||
// //check if there is any repetition in the products
|
||||
// $customerCartId = $customerCart->id ?? $customerCart['id'];
|
||||
|
||||
// //to check if the customer is also having some products saved in the pivot
|
||||
// //for the products.
|
||||
// $customerCartProducts = $this->cart->withProducts($customerCartId);
|
||||
|
||||
// //if there are products already in cart of that customer
|
||||
// if(isset($customerCartProducts)) {
|
||||
|
||||
// foreach($customerCartProducts as $previousCartProduct) {
|
||||
|
||||
// dump($customerCartProducts);
|
||||
|
||||
// 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'] = $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'] = $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();
|
||||
if(auth()->guard('customer')->check()) {
|
||||
Cart::remove($id);
|
||||
} else {
|
||||
Cart::guestUnitRemove($id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,12 +3,16 @@
|
|||
namespace Webkul\Cart\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Routing\Router;
|
||||
use Webkul\Customer\Http\Middleware\RedirectIfNotCustomer;
|
||||
use Illuminate\Foundation\AliasLoader;
|
||||
use Webkul\User\Http\Middleware\RedirectIfNotAdmin;
|
||||
use Webkul\Customer\Http\Middleware\RedirectIfNotCustomer;
|
||||
use Webkul\Cart\Facades\Cart;
|
||||
|
||||
class CartServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
public function boot(Router $router)
|
||||
{
|
||||
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
|
||||
|
|
@ -27,7 +31,24 @@ class CartServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
// $this->app->bind('datagrid', 'Webkul\Ui\DataGrid\DataGrid');
|
||||
$this->app->bind('Cart', 'Webkul\Cart\Cart');
|
||||
$this->registerFacades();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Bouncer as a singleton.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerFacades()
|
||||
{
|
||||
$loader = AliasLoader::getInstance();
|
||||
|
||||
$loader->alias('cart', Cart::class);
|
||||
|
||||
$this->app->singleton('cart', function () {
|
||||
return new cart();
|
||||
});
|
||||
|
||||
$this->app->bind('cart', 'Webkul\Cart\Cart');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,4 +56,34 @@ class CartRepository extends Repository
|
|||
|
||||
return $this->model->find($id)->with_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to attach
|
||||
* associations
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
// public function onlyAttach($id, $taxRates) {
|
||||
|
||||
// foreach($taxRates as $key => $value) {
|
||||
|
||||
// $this->model->findOrFail($id)->tax_rates()->attach($id, ['tax_category_id' => $id, 'tax_rate_id' => $value]);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Method to detach
|
||||
* and attach the
|
||||
* associations
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
// public function syncAndDetach($id, $taxRates) {
|
||||
// $this->model->findOrFail($id)->tax_rates()->detach();
|
||||
|
||||
// foreach($taxRates as $key => $value) {
|
||||
// $this->model->findOrFail($id)->tax_rates()->attach($id, ['tax_category_id' => $id, 'tax_rate_id' => $value]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ use Webkul\Core\Http\Middleware\Locale;
|
|||
use Webkul\User\Http\Middleware\RedirectIfNotAdmin;
|
||||
use Webkul\Customer\Http\Middleware\RedirectIfNotCustomer;
|
||||
use Webkul\Core\Core;
|
||||
use Webkul\Core\Facades\CoreFacade;
|
||||
use Webkul\Core\Facades\Core as CoreFacade;
|
||||
|
||||
class CoreServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "prashant-webkul",
|
||||
"name": "Prashant Singh",
|
||||
"email": "prashant.singh852@webkul.com"
|
||||
}
|
||||
],
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@ namespace Webkul\Customer\Http\Controllers;
|
|||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
|
||||
use Webkul\Customer\Models\Customer;
|
||||
use Webkul\Customer\Http\Listeners\CustomerEventsHandler;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Cookie;
|
||||
|
||||
use Cookie;
|
||||
use Cart;
|
||||
/**
|
||||
* Session controller for the user customer
|
||||
*
|
||||
|
|
@ -60,11 +62,10 @@ class SessionController extends Controller
|
|||
}
|
||||
|
||||
//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']));
|
||||
}
|
||||
|
||||
Event::fire('customer.after.login', $request->input('email'));
|
||||
|
||||
return redirect()->intended(route($this->_config['redirect']));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
|
|
|
|||
|
|
@ -27,12 +27,7 @@ class CustomerEventsHandler {
|
|||
* check emptiness and then
|
||||
* do the appropriate actions.
|
||||
*/
|
||||
Cart::getCart(1);
|
||||
// return redirect()->route('cart.merge');
|
||||
// if(Cookie::has('session_c')) {
|
||||
|
||||
// }
|
||||
|
||||
Cart::handleMerge();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
{
|
||||
"name": "webkul/laravel-shop",
|
||||
"license": "MIT",
|
||||
"description" : "Shop package for store front and customers",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Prashant",
|
||||
"email": "prashant@webkul.com"
|
||||
"name": "Prashant Singh",
|
||||
"email": "prashant.singh852@webkul.com"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ Route::group(['middleware' => ['web']], function () {
|
|||
|
||||
// //Routes for product cart
|
||||
|
||||
Route::post('products/guest/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@guestUnitAdd')->name('cart.guest.add');
|
||||
Route::post('products/guest/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@add')->name('cart.add');
|
||||
|
||||
Route::post('product/guest/cart/remove/{id}', 'Webkul\Cart\Http\Controllers\CartController@guestUnitRemove')->name('cart.guest.remove');
|
||||
Route::post('product/guest/cart/remove/{id}', 'Webkul\Cart\Http\Controllers\CartController@remove')->name('cart.remove');
|
||||
|
||||
Route::post('product/customer/cart/add/{id}', 'Webkul\Cart\Http\Controllers\CartController@add')->name('cart.customer.add');
|
||||
// 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::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');
|
||||
|
||||
|
|
|
|||
|
|
@ -127,7 +127,6 @@ body {
|
|||
.header-top {
|
||||
margin-bottom: 16px;
|
||||
display: flex;
|
||||
max-width: 80%;
|
||||
width: 100%;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@
|
|||
@include('shop::home.featured-products')
|
||||
|
||||
@include('shop::home.new-products')
|
||||
|
||||
|
||||
@include('shop::home.news-updates')
|
||||
@endsection
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@
|
|||
|
||||
</div>
|
||||
<div class="layouter">
|
||||
|
||||
<form method="POST" @auth('customer') action="{{ route('cart.customer.add', $product->id) }}" @endauth @guest action="{{ route('cart.guest.add', $product->id) }}" @endguest>
|
||||
{{ dd(session()->getId()) }}
|
||||
<form method="POST" action="{{ route('cart.add', $product->id) }}">
|
||||
@csrf()
|
||||
|
||||
<input type="hidden" name="product">
|
||||
|
|
|
|||
|
|
@ -21,11 +21,13 @@
|
|||
<input type="hidden" name="qty" value="1">
|
||||
|
||||
<input type="submit" class="btn btn-lg add-to-cart" value="Add to Cart">
|
||||
|
||||
{{-- <form>
|
||||
<input type="hidden" name="product_id" value="">
|
||||
<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>
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@
|
|||
<form onsubmit="return confirm('Are You Sure?');"
|
||||
@if(strtoupper($massoperation[ 'method'])=="GET" || strtoupper($massoperation['method'])=="POST" )
|
||||
method="{{ strtoupper($massoperation['method']) }}"
|
||||
|
||||
@else
|
||||
method="POST"
|
||||
@endif
|
||||
|
|
|
|||
|
|
@ -141,7 +141,6 @@ body {
|
|||
display: -webkit-box;
|
||||
display: -ms-flexbox;
|
||||
display: flex;
|
||||
max-width: 80%;
|
||||
width: 100%;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
|
|
|
|||
Loading…
Reference in New Issue