2018-09-06 06:20:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Cart\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
|
|
|
|
//Cart repositories
|
|
|
|
|
use Webkul\Cart\Repositories\CartRepository;
|
|
|
|
|
use Webkul\Cart\Repositories\CartProductRepository;
|
|
|
|
|
|
|
|
|
|
//Customer repositories
|
2018-09-10 09:31:34 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
|
|
|
|
use Cart;
|
2018-09-10 09:31:34 +00:00
|
|
|
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-13 11:06:17 +00:00
|
|
|
public function __construct(CartRepository $cart, CartProductRepository $cartProduct, CustomerRepository $customer) {
|
|
|
|
|
|
|
|
|
|
$this->middleware('customer')->except(['add', 'remove']);
|
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
|
|
|
|
|
*/
|
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-13 11:06:17 +00:00
|
|
|
if(auth()->guard('customer')->check()) {
|
|
|
|
|
Cart::add($id);
|
|
|
|
|
} else {
|
|
|
|
|
Cart::guestUnitAdd($id);
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|
2018-09-14 13:15:49 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-09-06 06:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-10 09:31:34 +00:00
|
|
|
public function remove($id) {
|
2018-09-11 11:19:40 +00:00
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
if(auth()->guard('customer')->check()) {
|
|
|
|
|
Cart::remove($id);
|
|
|
|
|
} else {
|
|
|
|
|
Cart::guestUnitRemove($id);
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|
2018-09-14 13:15:49 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|
2018-09-06 06:20:30 +00:00
|
|
|
}
|