2018-09-06 06:20:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-09-28 13:28:54 +00:00
|
|
|
namespace Webkul\Shop\Http\Controllers;
|
2018-09-06 06:20:30 +00:00
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2018-09-28 13:28:54 +00:00
|
|
|
use Webkul\Checkout\Repositories\CartRepository;
|
|
|
|
|
use Webkul\Checkout\Repositories\CartItemRepository;
|
2018-09-20 10:00:52 +00:00
|
|
|
use Webkul\Product\Repositories\ProductRepository;
|
2018-09-10 09:31:34 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-10-17 07:21:47 +00:00
|
|
|
use Webkul\Product\Helpers\ProductImage;
|
|
|
|
|
use Webkul\Product\Helpers\View as ProductView;
|
2018-09-13 11:06:17 +00:00
|
|
|
use Cart;
|
2018-09-06 06:20:30 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-10-10 10:26:27 +00:00
|
|
|
* Cart controller for the customer and guest users for adding and
|
|
|
|
|
* removing the products in the cart.
|
2018-09-06 06:20:30 +00:00
|
|
|
*
|
2018-10-26 07:28:38 +00:00
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
2018-09-06 06:20:30 +00:00
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class CartController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-10 10:26:27 +00:00
|
|
|
* Protected Variables that holds instances of the repository classes.
|
2018-09-06 06:20:30 +00:00
|
|
|
*
|
2018-09-25 08:37:34 +00:00
|
|
|
* @param Array $_config
|
|
|
|
|
* @param $cart
|
|
|
|
|
* @param $cartItem
|
|
|
|
|
* @param $customer
|
|
|
|
|
* @param $product
|
|
|
|
|
* @param $productImage
|
|
|
|
|
* @param $productView
|
2018-09-06 06:20:30 +00:00
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
|
|
protected $cart;
|
|
|
|
|
|
2018-09-20 08:33:28 +00:00
|
|
|
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;
|
|
|
|
|
|
2018-09-24 12:05:09 +00:00
|
|
|
protected $productView;
|
|
|
|
|
|
2018-09-25 08:37:34 +00:00
|
|
|
public function __construct(
|
|
|
|
|
CartRepository $cart,
|
|
|
|
|
CartItemRepository $cartItem,
|
|
|
|
|
CustomerRepository $customer,
|
|
|
|
|
ProductRepository $product,
|
|
|
|
|
ProductImage $productImage,
|
2018-09-26 04:21:14 +00:00
|
|
|
ProductView $productView
|
|
|
|
|
) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
$this->middleware('customer')->only(['moveToWishlist']);
|
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
|
|
|
|
2018-09-20 08:33:28 +00:00
|
|
|
$this->cartItem = $cartItem;
|
2018-09-20 10:00:52 +00:00
|
|
|
|
|
|
|
|
$this->product = $product;
|
2018-09-24 12:05:09 +00:00
|
|
|
|
|
|
|
|
$this->productImage = $productImage;
|
|
|
|
|
|
|
|
|
|
$this->productView = $productView;
|
|
|
|
|
|
|
|
|
|
$this->_config = request('_config');
|
2018-09-10 09:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
2018-10-04 06:43:49 +00:00
|
|
|
* Method to populate the cart page which will be populated before the checkout process.
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function index() {
|
|
|
|
|
return view($this->_config['view'])->with('cart', Cart::getCart());
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-11 11:19:40 +00:00
|
|
|
/**
|
2018-10-10 10:26:27 +00:00
|
|
|
* Function for guests user to add the product in the cart.
|
2018-09-11 11:19:40 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
2018-09-10 09:31:34 +00:00
|
|
|
|
2018-09-11 11:19:40 +00:00
|
|
|
public function add($id) {
|
2018-09-20 08:33:28 +00:00
|
|
|
$data = request()->input();
|
2018-10-26 07:05:19 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
Cart::add($id, $data);
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-10-18 09:31:19 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2018-09-14 13:15:49 +00:00
|
|
|
return redirect()->back();
|
2018-09-06 06:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
/**
|
2018-10-10 10:26:27 +00:00
|
|
|
* Removes the item from the cart if it exists
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
2018-09-27 19:04:56 +00:00
|
|
|
public function remove($itemId) {
|
2018-09-28 12:55:48 +00:00
|
|
|
Cart::removeItem($itemId);
|
|
|
|
|
|
2018-10-18 09:31:19 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-10 10:26:27 +00:00
|
|
|
* Updates the quantity of the items present in the cart.
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
|
|
|
|
public function updateBeforeCheckout() {
|
|
|
|
|
$data = request()->except('_token');
|
|
|
|
|
|
2018-10-04 06:43:49 +00:00
|
|
|
foreach($data['qty'] as $id => $quantity) {
|
|
|
|
|
if($quantity <= 0) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.illegal'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-12 07:21:39 +00:00
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
Cart::update($data);
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-10-18 09:31:19 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2018-09-14 13:15:49 +00:00
|
|
|
return redirect()->back();
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|
2018-09-17 08:27:32 +00:00
|
|
|
|
2018-10-12 11:15:55 +00:00
|
|
|
/**
|
|
|
|
|
* Add the configurable product
|
|
|
|
|
* to the cart.
|
|
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
2018-10-30 11:22:36 +00:00
|
|
|
public function addConfigurable($slug) {
|
2018-10-12 11:15:55 +00:00
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.add-config-warning'));
|
|
|
|
|
return redirect()->route('shop.products.index', $slug);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
public function buyNow($id) {
|
2018-10-26 07:05:19 +00:00
|
|
|
$result = Cart::proceedForBuyNow($id);
|
|
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2018-10-26 07:05:19 +00:00
|
|
|
if(!$result) {
|
|
|
|
|
return redirect()->back();
|
2018-10-30 11:22:36 +00:00
|
|
|
} else {
|
|
|
|
|
return redirect()->route('shop.checkout.onepage.index');
|
2018-10-26 07:05:19 +00:00
|
|
|
}
|
2018-09-27 10:07:54 +00:00
|
|
|
}
|
2018-10-30 04:31:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to move a already added product to wishlist
|
|
|
|
|
* will run only on customer authentication.
|
|
|
|
|
*
|
|
|
|
|
* @param instance cartItem $id
|
|
|
|
|
*/
|
|
|
|
|
public function moveToWishlist($id) {
|
|
|
|
|
$result = Cart::moveToWishlist($id);
|
|
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
if($result) {
|
|
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
|
|
|
|
session()->flash('success', 'Item Successfully Moved To Wishlist');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('warning', 'Cannot move item to wishlist');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-10-30 04:31:01 +00:00
|
|
|
}
|
2018-09-06 06:20:30 +00:00
|
|
|
}
|