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

119 lines
2.8 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;
//Cart repositories
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
2018-09-20 10:00:52 +00:00
//Product Repository
use Webkul\Product\Repositories\ProductRepository;
//Customer repositories
2018-09-10 09:31:34 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
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;
protected $cartItem;
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-20 10:00:52 +00:00
protected $product;
public function __construct(CartRepository $cart, CartItemRepository $cartItem, CustomerRepository $customer, ProductRepository $product) {
$this->middleware('customer')->except(['add', 'remove', 'test']);
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->cartItem = $cartItem;
2018-09-20 10:00:52 +00:00
$this->product = $product;
2018-09-10 09:31:34 +00:00
}
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) {
$data = request()->input();
2018-09-20 10:00:52 +00:00
// dd($data);
if(!isset($data['is_configurable']) || !isset($data['product']) ||!isset($data['quantity'])) {
session()->flash('error', 'Cannot Product Due to User\'s miscreancy in system\'s integrity');
return redirect()->back();
}
if($data['is_configurable'] == "false") {
$data['price'] = $this->product->findOneByField('id', $data['product'])->price;
} else {
$id = $data['selected_configurable_option'];
$data['price'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->price;
}
if(auth()->guard('customer')->check()) {
Cart::add($id, $data);
} else {
Cart::guestUnitAdd($id, $data);
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
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
}
/**
* This is a test for
* relationship existence
* from cart item to product
*
* @return Array
*/
public function test() {
2018-09-20 15:12:11 +00:00
$data['product_id'] = 60;
$data['quantity'] = 7;
$data['price'] = 1600.00;
dd($this->cart->createItem(80, $data));
}
2018-09-06 06:20:30 +00:00
}