sarga/packages/Webkul/Cart/src/Http/Controllers/CartController.php

338 lines
9.7 KiB
PHP
Raw Normal View History

2018-09-06 06:20:30 +00:00
<?php
namespace Webkul\Cart\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2018-09-11 11:19:40 +00:00
// use Illuminate\Routing\Controller;
2018-09-10 09:31:34 +00:00
use Webkul\Cart\Repositories\CartRepository as Cart;
use Webkul\Cart\Repositories\CartProductRepository as cartProduct;
use Webkul\Customer\Repositories\CustomerRepository;
use Cookie;
2018-09-06 06:20:30 +00:00
/**
* Cart controller for the customer
* and guest users for adding and
* removing the products in the
* cart.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $_config;
protected $cart;
2018-09-10 09:31:34 +00:00
protected $cartProduct;
2018-09-06 06:20:30 +00:00
2018-09-10 09:31:34 +00:00
protected $customer;
2018-09-06 06:20:30 +00:00
2018-09-10 09:31:34 +00:00
public function __construct(Cart $cart, cartProduct $cartProduct, CustomerRepository $customer) {
2018-09-11 11:19:40 +00:00
$this->middleware('customer')->except(['guestUnitAdd', 'guestUnitRemove']);
2018-09-10 09:31:34 +00:00
$this->customer = $customer;
2018-09-06 06:20:30 +00:00
$this->cart = $cart;
2018-09-10 09:31:34 +00:00
$this->cartProduct = $cartProduct;
}
2018-09-11 11:19:40 +00:00
/**
* 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
2018-09-10 09:31:34 +00:00
$products = array();
2018-09-11 11:19:40 +00:00
$minutes = 5;
if(Cookie::has('session_c')) {
$products = unserialize(Cookie::get('session_c'));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
foreach($products as $key => $value) {
if($value == $id) {
dump($products);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
dd('product already in the cart');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
return redirect()->back();
}
}
array_push($products, $id);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
Cookie::queue('session_c', serialize($products));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
return redirect()->back();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
} else {
array_push($products, $id);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
Cookie::queue('session_c', serialize($products), $minutes);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// dump($products);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
return redirect()->back();
}
}
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
/**
* Function for guests
* user to remove the product
* in the cart.
*
* @return Mixed
*/
public function guestUnitRemove($id) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
//remove the products here
if(Cookie::has('session_c')) {
$products = unserialize(Cookie::get('session_c'));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
foreach($products as $key => $value) {
if($value == $id) {
unset($products[$key]);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
array_push($products, $id);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
Cookie::queue('session_c', serialize($products));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
return redirect()->back();
2018-09-10 09:31:34 +00:00
}
2018-09-11 11:19:40 +00:00
}
}
}
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
/*handle the after login event for the customers
when their are pruoducts in the session or cookie
of the logged in user.*/
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
public function add($id) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$products = array();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$customerLoggedIn = auth()->guard('customer')->check();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
//customer is authenticated
if ($customerLoggedIn) {
//assuming that there is data in cookie and customer's cart also.
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$data['customer_id'] = auth()->guard('customer')->user()->id;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$data['channel_id'] = core()->getCurrentChannel()->id;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$customerCart = $this->cart->findOneByField('customer_id', $data['customer_id']);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
//if there are products already in cart of that customer.
$customerCartId = $customerCart->id ?? $customerCart['id'];
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
$customerCartProducts = $this->cart->withProducts($customerCartId);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
if (isset($customerCartProducts)) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
foreach ($customerCartProducts as $previousCartProduct) {
2018-09-10 09:31:34 +00:00
if($previousCartProduct->id == $id) {
2018-09-11 11:19:40 +00:00
dd('product already exists in cart');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
session()->flash('error', 'Product already exists in cart');
2018-09-10 09:31:34 +00:00
return redirect()->back();
}
}
2018-09-11 11:19:40 +00:00
//add the product in the cart
2018-09-10 09:31:34 +00:00
$product['product_id'] = $id;
$product['quantity'] = 1;
2018-09-11 11:19:40 +00:00
$product['cart_id'] = $customerCartId;
2018-09-10 09:31:34 +00:00
$this->cartProduct->create($product);
return redirect()->back();
2018-09-11 11:19:40 +00:00
}
}
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// //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'));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// //check if there is any repetition in the products
// $customerCartId = $customerCart->id ?? $customerCart['id'];
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// //to check if the customer is also having some products saved in the pivot
// //for the products.
// $customerCartProducts = $this->cart->withProducts($customerCartId);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// //if there are products already in cart of that customer
// if(isset($customerCartProducts)) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// foreach($customerCartProducts as $previousCartProduct) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// dump($customerCartProducts);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// foreach($cookieProducts as $key => $cookieProduct) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// if($previousCartProduct->id == $cookieProduct) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// unset($cookieProducts[$key]);
// }
// }
// }
// }
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// /*if the above block executes it will remove duplicates
// else product in cookies will be stored in the database.*/
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// foreach($cookieProducts as $key => $cookieProduct) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['product_id'] = $cookieProduct;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['quantity'] = 1;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['cart_id'] = $customerCartId;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $this->cartProduct->create($product);
// }
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// dump('Products in the cart synced.');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// return redirect()->back();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// } 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);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// foreach($customerCartProducts as $previousCartProduct) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// if($previousCartProduct->id == $id) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// dd('product already in the cart::AUTH');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// return redirect()->back();
// }
// }
// $product['product_id'] = $id;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['quantity'] = 1;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['cart_id'] = $customerCartId;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $this->cartProduct->create($product);
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// dump('new item added in the cart');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// return redirect()->back();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// } else if(!isset($customerCart) && isset($cartCookie)) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $products = unserialize(Cookie::get('session_c'));
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// if ($cart = $this->cart->create($data)) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// foreach($products as $product) {
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['product_id'] = $id;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['quantity'] = 1;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $product['cart_id'] = $cart;
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// $this->cartProduct->create($product);
// }
// return redirect()->back();
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// } else {
// session()->flash('error', 'Cannot Add Your Items To Cart');
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
// return redirect()->back();
// }
2018-09-10 09:31:34 +00:00
// }
// }
2018-09-06 06:20:30 +00:00
}
2018-09-11 11:19:40 +00:00
/**
* use detach to remove the
* current product from cart tables
*
* @return Mixed
*/
2018-09-10 09:31:34 +00:00
public function remove($id) {
2018-09-11 11:19:40 +00:00
2018-09-10 09:31:34 +00:00
dd("Removing Item from Cart");
2018-09-06 06:20:30 +00:00
}
2018-09-11 11:19:40 +00:00
/**
* 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();
}
}
2018-09-06 06:20:30 +00:00
}