sarga/packages/Webkul/Customer/src/Http/Controllers/WishlistController.php

197 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Webkul\Product\Repositories\ProductRepository;
2018-10-05 12:00:48 +00:00
use Webkul\Customer\Repositories\WishlistRepository;
use Cart;
/**
* Customer controller
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
2018-10-11 14:25:59 +00:00
class WishlistController extends Controller
{
2019-07-01 11:33:36 +00:00
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
2018-10-05 12:00:48 +00:00
2019-07-01 11:33:36 +00:00
/**
* ProductRepository object
*
* @var Object
*/
protected $wishlistRepository;
2019-07-01 11:33:36 +00:00
/**
* WishlistRepository object
*
* @var Object
*/
protected $productRepository;
2018-10-05 12:00:48 +00:00
/**
2019-07-01 11:33:36 +00:00
* Create a new controller instance.
2018-10-05 12:00:48 +00:00
*
2019-07-01 11:33:36 +00:00
* @param \Webkul\Customer\Repositories\WishlistRepository $wishlistRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @return void
2018-10-05 12:00:48 +00:00
*/
2019-07-01 11:33:36 +00:00
public function __construct(
WishlistRepository $wishlistRepository,
ProductRepository $productRepository
)
{
$this->middleware('customer');
$this->_config = request('_config');
2019-07-01 11:33:36 +00:00
$this->wishlistRepository = $wishlistRepository;
2019-07-01 11:33:36 +00:00
$this->productRepository = $productRepository;
}
/**
2018-10-05 12:00:48 +00:00
* Displays the listing resources if the customer having items in wishlist.
2019-12-12 11:39:50 +00:00
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
2019-07-01 11:33:36 +00:00
public function index()
{
2019-12-12 11:39:50 +00:00
$wishlistItems = $this->wishlistRepository->getCustomerWhishlist();
2018-10-05 12:00:48 +00:00
return view($this->_config['view'])->with('items', $wishlistItems);
}
/**
* Function to add item to the wishlist.
*
* @param integer $itemId
*/
2019-07-01 11:33:36 +00:00
public function add($itemId)
{
$product = $this->productRepository->findOneByField('id', $itemId);
2019-08-19 09:30:24 +00:00
if (! $product->status)
return redirect()->back();
2018-10-05 12:00:48 +00:00
$data = [
'channel_id' => core()->getCurrentChannel()->id,
'product_id' => $itemId,
'customer_id' => auth()->guard('customer')->user()->id
];
2019-07-01 11:33:36 +00:00
$checked = $this->wishlistRepository->findWhere([
'channel_id' => core()->getCurrentChannel()->id,
'product_id' => $itemId,
'customer_id' => auth()->guard('customer')->user()->id
]);
2018-10-05 12:00:48 +00:00
2018-11-24 09:56:31 +00:00
//accidental case if some one adds id of the product in the anchor tag amd gives id of a variant.
2019-01-15 11:54:41 +00:00
if ($product->parent_id != null) {
2019-07-01 11:33:36 +00:00
$product = $this->productRepository->findOneByField('id', $product->parent_id);
2019-05-27 12:55:30 +00:00
$data['product_id'] = $product->id;
2018-11-24 09:56:31 +00:00
}
2019-01-15 11:54:41 +00:00
if ($checked->isEmpty()) {
2019-07-01 11:33:36 +00:00
if ($this->wishlistRepository->create($data)) {
2018-10-05 12:00:48 +00:00
session()->flash('success', trans('customer::app.wishlist.success'));
return redirect()->back();
} else {
session()->flash('error', trans('customer::app.wishlist.failure'));
return redirect()->back();
}
} else {
session()->flash('warning', trans('customer::app.wishlist.already'));
return redirect()->back();
}
}
/**
* Function to remove item to the wishlist.
*
* @param integer $itemId
*/
2019-07-01 11:33:36 +00:00
public function remove($itemId)
{
2019-03-31 20:02:58 +00:00
$customerWishlistItems = auth()->guard('customer')->user()->wishlist_items;
2018-10-05 12:00:48 +00:00
2019-07-01 11:33:36 +00:00
foreach ($customerWishlistItems as $customerWishlistItem) {
if ($itemId == $customerWishlistItem->id) {
$this->wishlistRepository->delete($itemId);
2019-02-25 04:15:12 +00:00
session()->flash('success', trans('customer::app.wishlist.removed'));
return redirect()->back();
}
2018-10-05 12:00:48 +00:00
}
2019-03-31 20:02:58 +00:00
session()->flash('error', trans('customer::app.wishlist.remove-fail'));
return redirect()->back();
}
/**
* Function to move item from wishlist to cart.
*
* @param integer $itemId
*/
2019-07-01 11:33:36 +00:00
public function move($itemId)
{
2019-08-19 09:30:24 +00:00
$wishlistItem = $this->wishlistRepository->findOneWhere([
'id' => $itemId,
'customer_id' => auth()->guard('customer')->user()->id
]);
2019-03-31 20:02:58 +00:00
2019-08-19 09:30:24 +00:00
if (! $wishlistItem)
abort(404);
2019-03-31 20:02:58 +00:00
try {
$result = Cart::moveToCart($wishlistItem);
2019-08-19 09:30:24 +00:00
if ($result) {
2020-01-13 07:14:36 +00:00
session()->flash('success', trans('shop::app.customer.account.wishlist.moved'));
} else {
2020-01-13 06:56:53 +00:00
session()->flash('info', trans('shop::app.checkout.cart.integrity.missing_options'));
return redirect()->route('shop.productOrCategory.index', $wishlistItem->product->url_key);
}
2019-08-19 09:30:24 +00:00
return redirect()->back();
} catch (\Exception $e) {
session()->flash('warning', $e->getMessage());
2019-08-19 09:30:24 +00:00
2019-12-27 08:00:05 +00:00
return redirect()->route('shop.productOrCategory.index', ['slugOrPath' => $wishlistItem->product->url_key]);
}
}
/**
* Function to remove all of the items items in the customer's wishlist
*
* @return Mixed Response & Boolean
*/
2019-07-01 11:33:36 +00:00
public function removeAll()
{
$wishlistItems = auth()->guard('customer')->user()->wishlist_items;
2019-01-15 11:54:41 +00:00
if ($wishlistItems->count() > 0) {
foreach ($wishlistItems as $wishlistItem) {
2019-07-01 11:33:36 +00:00
$this->wishlistRepository->delete($wishlistItem->id);
}
}
session()->flash('success', trans('customer::app.wishlist.remove-all-success'));
2019-07-01 11:33:36 +00:00
return redirect()->back();
}
2019-03-31 20:02:58 +00:00
}