2018-08-23 13:11:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Customer\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-10-15 14:48:58 +00:00
|
|
|
use Webkul\Product\Repositories\ProductRepository;
|
2018-10-05 12:00:48 +00:00
|
|
|
use Webkul\Customer\Repositories\WishlistRepository;
|
2018-10-15 14:48:58 +00:00
|
|
|
use Cart;
|
2018-08-23 13:11:56 +00:00
|
|
|
use Auth;
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-27 11:13:57 +00:00
|
|
|
* Customer controlller for the customer basically for the tasks of customers which will be done
|
|
|
|
|
* after customer authenticastion.
|
2018-08-23 13:11:56 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
2018-08-23 13:11:56 +00:00
|
|
|
class WishlistController extends Controller
|
|
|
|
|
{
|
|
|
|
|
protected $_config;
|
2018-10-05 12:00:48 +00:00
|
|
|
|
2018-08-23 13:11:56 +00:00
|
|
|
protected $customer;
|
|
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
protected $wishlist;
|
2018-08-23 13:11:56 +00:00
|
|
|
|
2018-10-15 14:48:58 +00:00
|
|
|
protected $product;
|
|
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
/**
|
|
|
|
|
* Initializes the required repository instances.
|
|
|
|
|
*
|
|
|
|
|
* @param $customer
|
|
|
|
|
* @param $wishlist
|
|
|
|
|
*/
|
2018-10-15 14:48:58 +00:00
|
|
|
public function __construct(CustomerRepository $customer, WishlistRepository $wishlist, ProductRepository $product)
|
2018-08-23 13:11:56 +00:00
|
|
|
{
|
2018-08-31 06:03:11 +00:00
|
|
|
$this->middleware('customer');
|
|
|
|
|
|
2018-08-23 13:11:56 +00:00
|
|
|
$this->_config = request('_config');
|
2018-08-31 06:03:11 +00:00
|
|
|
|
2018-08-23 13:11:56 +00:00
|
|
|
$this->customer = $customer;
|
2018-10-05 12:00:48 +00:00
|
|
|
|
|
|
|
|
$this->wishlist = $wishlist;
|
2018-10-15 14:48:58 +00:00
|
|
|
|
|
|
|
|
$this->product = $product;
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-05 12:00:48 +00:00
|
|
|
* Displays the listing resources if the customer having items in wishlist.
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
|
|
|
|
public function index() {
|
2018-11-16 11:51:10 +00:00
|
|
|
$wishlistItems = $this->wishlist->findWhere([
|
|
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
|
|
|
|
'customer_id' => auth()->guard('customer')->user()->id]
|
|
|
|
|
);
|
2018-11-28 07:29:08 +00:00
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
return view($this->_config['view'])->with('items', $wishlistItems);
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 10:28:44 +00:00
|
|
|
/**
|
|
|
|
|
* Function to add item to the wishlist.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
2018-10-04 14:16:23 +00:00
|
|
|
public function add($itemId) {
|
2018-10-15 14:48:58 +00:00
|
|
|
$product = $this->product->findOneByField('id', $itemId);
|
|
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
$data = [
|
|
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
|
|
|
|
'product_id' => $itemId,
|
|
|
|
|
'customer_id' => auth()->guard('customer')->user()->id
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$checked = $this->wishlist->findWhere(['channel_id' => core()->getCurrentChannel()->id, 'product_id' => $itemId, 'customer_id' => auth()->guard('customer')->user()->id]);
|
|
|
|
|
|
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.
|
2018-11-28 07:29:08 +00:00
|
|
|
if($product->parent_id != null) {
|
2018-11-24 09:56:31 +00:00
|
|
|
$product = $this->product->findOneByField('id', $product->parent_id);
|
|
|
|
|
$data['product_id'] = $product->parent_id;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
if($checked->isEmpty()) {
|
|
|
|
|
if($this->wishlist->create($data)) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
2018-10-04 10:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to remove item to the wishlist.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
|
|
|
|
public function remove($itemId) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$result = $this->wishlist->deleteWhere(['customer_id' => auth()->guard('customer')->user()->id, 'channel_id' => core()->getCurrentChannel()->id, 'id' => $itemId]);
|
2018-10-05 12:00:48 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
if($result) {
|
|
|
|
|
session()->flash('success', trans('customer::app.wishlist.removed'));
|
2018-10-05 12:00:48 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('error', trans('customer::app.wishlist.remove-fail'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-10-04 10:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-15 14:48:58 +00:00
|
|
|
/**
|
|
|
|
|
* Function to move item from wishlist to cart.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
2018-11-16 11:51:10 +00:00
|
|
|
public function move($itemId) {
|
|
|
|
|
$wishlistItem = $this->wishlist->findOneByField('id', $itemId);
|
|
|
|
|
$result = Cart::moveToCart($wishlistItem);
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
if($result == 1) {
|
|
|
|
|
if($wishlistItem->delete()) {
|
|
|
|
|
session()->flash('success', trans('shop::app.wishlist.moved'));
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2018-10-22 10:40:05 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2018-10-18 12:43:45 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
} else {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('error', trans('shop::app.wishlist.move-error'));
|
2018-10-18 12:43:45 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-11-16 11:51:10 +00:00
|
|
|
} else if($result == 0) {
|
|
|
|
|
Session('error', trans('shop::app.wishlist.error'));
|
2018-10-15 14:48:58 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-11-16 11:51:10 +00:00
|
|
|
} else if($result == -1) {
|
|
|
|
|
if(!$wishlistItem->delete()) {
|
|
|
|
|
session()->flash('error', trans('shop::app.wishlist.move-error'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 07:29:08 +00:00
|
|
|
session()->flash('info', trans('shop::app.checkout.cart.add-config-warning'));
|
2018-11-16 11:51:10 +00:00
|
|
|
|
|
|
|
|
return redirect()->route('shop.products.index', $wishlistItem->product->url_key);
|
2018-10-15 14:48:58 +00:00
|
|
|
}
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
2018-11-16 11:51:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to remove all of the items items in the customer's wishlist
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed Response & Boolean
|
|
|
|
|
*/
|
|
|
|
|
public function removeAll() {
|
|
|
|
|
$wishlistItems = auth()->guard('customer')->user()->wishlist_items;
|
|
|
|
|
|
|
|
|
|
if($wishlistItems->count() > 0) {
|
|
|
|
|
foreach($wishlistItems as $wishlistItem) {
|
|
|
|
|
$this->wishlist->delete($wishlistItem->id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('customer::app.wishlist.remove-all-success'));
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-10-18 12:12:41 +00:00
|
|
|
}
|