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

264 lines
7.3 KiB
PHP
Raw Normal View History

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
2018-09-28 13:28:54 +00:00
use Webkul\Checkout\Repositories\CartItemRepository;
2018-09-20 10:00:52 +00:00
use Webkul\Product\Repositories\ProductRepository;
2019-04-02 07:54:11 +00:00
use Webkul\Customer\Repositories\WishlistRepository;
2019-01-07 10:30:28 +00:00
use Illuminate\Support\Facades\Event;
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
*
* @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
{
/**
2019-06-28 14:18:52 +00:00
* Contains route related configuration
2018-09-06 06:20:30 +00:00
*
2019-06-28 14:18:52 +00:00
* @var array
2018-09-06 06:20:30 +00:00
*/
protected $_config;
2019-06-28 14:18:52 +00:00
/**
* CartItemRepository object
*
* @var Object
*/
protected $cartItemRepository;
2019-06-28 14:18:52 +00:00
/**
* ProductRepository object
*
* @var Object
*/
protected $productRepository;
2019-06-28 14:18:52 +00:00
/**
* WishlistRepository Repository object
*
* @var Object
*/
protected $wishlistRepository;
2019-06-28 14:18:52 +00:00
/**
* @var boolean
*/
2019-03-28 10:06:48 +00:00
protected $suppressFlash = false;
2019-04-02 07:54:11 +00:00
/**
2019-06-28 14:18:52 +00:00
* Create a new controller instance.
2019-04-02 07:54:11 +00:00
*
2019-06-28 14:18:52 +00:00
* @param \Webkul\Checkout\Repositories\CartItemRepository $cartItemRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Customer\Repositories\CartItemRepository $wishlistRepository
* @return void
2019-04-02 07:54:11 +00:00
*/
public function __construct(
2019-06-28 14:18:52 +00:00
CartItemRepository $cartItemRepository,
ProductRepository $productRepository,
WishlistRepository $wishlistRepository
)
{
$this->middleware('customer')->only(['moveToWishlist']);
2018-09-06 06:20:30 +00:00
2019-06-28 14:18:52 +00:00
$this->cartItemRepository = $cartItemRepository;
2018-09-10 09:31:34 +00:00
2019-06-28 14:18:52 +00:00
$this->productRepository = $productRepository;
2018-09-20 10:00:52 +00:00
2019-06-28 14:18:52 +00:00
$this->wishlistRepository = $wishlistRepository;
2019-04-02 07:54:11 +00:00
$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
*/
2019-01-07 10:30:28 +00:00
public function index()
{
2018-09-26 04:21:14 +00:00
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
*/
2019-01-07 10:30:28 +00:00
public function add($id)
{
2019-01-18 11:10:40 +00:00
try {
2019-06-28 14:18:52 +00:00
$product = $this->productRepository->find($id);
$data = request()->all();
if ($product->type == 'downloadable' && ! isset($data['links'])) {
session()->flash('warning', trans('shop::app.checkout.cart.integrity.missing_links'));
return redirect()->route('shop.products.index', $product->url_key);
} else if ($product->type == 'configurable'
&& (! isset($data['selected_configurable_option']) || ! $data['selected_configurable_option'])) {
session()->flash('warning', trans('shop::app.checkout.cart.add-config-warning'));
return redirect()->route('shop.products.index', $product->url_key);
}
2019-01-18 11:10:40 +00:00
Event::fire('checkout.cart.add.before', $id);
2019-01-07 10:30:28 +00:00
2019-01-18 11:10:40 +00:00
$result = Cart::add($id, request()->except('_token'));
2018-10-26 07:05:19 +00:00
2019-01-18 11:10:40 +00:00
Event::fire('checkout.cart.add.after', $result);
2019-01-07 10:30:28 +00:00
Cart::collectTotals();
2019-01-18 11:10:40 +00:00
if ($result) {
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
2019-04-02 07:54:11 +00:00
if (auth()->guard('customer')->user()) {
$customer = auth()->guard('customer')->user();
if (count($customer->wishlist_items)) {
foreach ($customer->wishlist_items as $wishlist) {
if ($wishlist->product_id == $id) {
2019-06-28 14:18:52 +00:00
$this->wishlistRepository->delete($wishlist->id);
2019-04-02 07:54:11 +00:00
}
}
}
}
2019-03-28 08:59:45 +00:00
return redirect()->back();
2019-01-18 11:10:40 +00:00
} else {
session()->flash('warning', trans('shop::app.checkout.cart.item.error-add'));
2018-09-14 13:15:49 +00:00
return redirect()->back();
}
2018-10-18 09:31:19 +00:00
2019-01-18 11:10:40 +00:00
return redirect()->route($this->_config['redirect']);
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
2019-01-29 10:35:30 +00:00
2019-01-18 11:10:40 +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
*/
2019-01-07 10:30:28 +00:00
public function remove($itemId)
{
Event::fire('checkout.cart.delete.before', $itemId);
2018-09-28 12:55:48 +00:00
Cart::removeItem($itemId);
2019-01-07 10:30:28 +00:00
Event::fire('checkout.cart.delete.after', $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
*/
2019-01-07 10:30:28 +00:00
public function updateBeforeCheckout()
{
2019-06-07 11:06:50 +00:00
try {
$request = request()->except('_token');
2018-09-28 12:55:48 +00:00
2019-06-07 11:06:50 +00:00
foreach ($request['qty'] as $id => $quantity) {
if ($quantity <= 0) {
session()->flash('warning', trans('shop::app.checkout.cart.quantity.illegal'));
2018-10-04 06:43:49 +00:00
2019-06-07 11:06:50 +00:00
return redirect()->back();
}
2018-10-04 06:43:49 +00:00
}
2019-06-07 11:06:50 +00:00
foreach ($request['qty'] as $key => $value) {
2019-06-28 14:18:52 +00:00
$item = $this->cartItemRepository->findOneByField('id', $key);
2019-06-07 11:06:50 +00:00
$data['quantity'] = $value;
2019-06-07 11:06:50 +00:00
Event::fire('checkout.cart.update.before', $item);
2019-01-07 10:30:28 +00:00
2019-06-07 11:06:50 +00:00
$result = Cart::updateItem($item->product_id, $data, $key);
2019-03-28 10:06:48 +00:00
2019-06-07 11:06:50 +00:00
if ($result == false) {
$this->suppressFlash = true;
}
2019-06-07 11:06:50 +00:00
Event::fire('checkout.cart.update.after', $item);
2019-01-07 10:30:28 +00:00
2019-06-07 11:06:50 +00:00
unset($item);
unset($data);
}
2018-09-14 13:15:49 +00:00
2019-06-07 11:06:50 +00:00
Cart::collectTotals();
2018-10-18 09:31:19 +00:00
2019-06-07 11:06:50 +00:00
if ($this->suppressFlash) {
session()->forget('success');
session()->forget('warning');
session()->flash('info', trans('shop::app.checkout.cart.partial-cart-update'));
}
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
2019-03-28 10:06:48 +00:00
}
2018-09-14 13:15:49 +00:00
return redirect()->back();
2018-09-11 11:19:40 +00:00
}
public function buyNow($id, $quantity = 1)
2019-01-07 10:30:28 +00:00
{
try {
Event::fire('checkout.cart.add.before', $id);
$result = Cart::proceedToBuyNow($id, $quantity);
2019-01-18 11:10:40 +00:00
Event::fire('checkout.cart.add.after', $result);
2018-10-26 07:05:19 +00:00
Cart::collectTotals();
2019-06-26 11:10:15 +00:00
if (! $result) {
return redirect()->back();
} else {
return redirect()->route('shop.checkout.onepage.index');
}
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
2018-10-30 11:22:36 +00:00
2018-10-26 07:05:19 +00:00
return redirect()->back();
}
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
*/
2019-01-07 10:30:28 +00:00
public function moveToWishlist($id)
{
2018-10-30 04:31:01 +00:00
$result = Cart::moveToWishlist($id);
2019-01-15 11:54:41 +00:00
if (! $result) {
2018-10-30 11:22:36 +00:00
Cart::collectTotals();
session()->flash('success', trans('shop::app.wishlist.moved'));
2018-10-30 11:22:36 +00:00
return redirect()->back();
} else {
session()->flash('warning', trans('shop::app.wishlist.move-error'));
2018-10-30 11:22:36 +00:00
return redirect()->back();
}
2018-10-30 04:31:01 +00:00
}
2018-09-06 06:20:30 +00:00
}