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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Customer controlller for the customer
|
|
|
|
|
* basically for the tasks of customers
|
|
|
|
|
* which will be done after customer
|
|
|
|
|
* authenticastion.
|
|
|
|
|
*
|
|
|
|
|
* @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-10-10 05:50:47 +00:00
|
|
|
$wishlists = $this->wishlist->findWhere([
|
|
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
|
|
|
|
'customer_id' => auth()->guard('customer')->user()->id]
|
|
|
|
|
);
|
2018-08-23 13:11:56 +00:00
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
$wishlistItems = array();
|
2018-08-23 13:11:56 +00:00
|
|
|
|
2018-10-05 12:00:48 +00:00
|
|
|
foreach($wishlists as $wishlist) {
|
|
|
|
|
array_push($wishlistItems, $this->wishlist->getItemsWithProducts($wishlist->id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if($product->type == "configurable") {
|
|
|
|
|
$slug = $product->url_key;
|
|
|
|
|
|
|
|
|
|
session()->flash('warning', trans('customer::app.wishlist.select-options'));
|
|
|
|
|
|
|
|
|
|
return redirect()->route('shop.products.index', $slug);
|
|
|
|
|
}
|
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]);
|
|
|
|
|
|
|
|
|
|
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-10-05 12:00:48 +00:00
|
|
|
|
|
|
|
|
if($this->wishlist->deleteWhere(['customer_id' => auth()->guard('customer')->user()->id, 'channel_id' => core()->getCurrentChannel()->id, 'product_id' => $itemId])) {
|
|
|
|
|
session()->flash('success', trans('customer::app.wishlist.remove'));
|
|
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Add the configurable product
|
|
|
|
|
* to the wishlist.
|
|
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
|
|
|
|
public function addconfigurable($urlkey) {
|
|
|
|
|
dd($urlkey);
|
|
|
|
|
session()->flash('warning', trans('Select options before adding to wishlist'));
|
|
|
|
|
return redirect()->route('shop.products.index', $urlkey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to move item from wishlist to cart.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
|
|
|
|
public function moveAll() {
|
|
|
|
|
Cart::moveAllToCart();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 10:28:44 +00:00
|
|
|
/**
|
|
|
|
|
* Function to move item from wishlist to cart.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $itemId
|
|
|
|
|
*/
|
2018-10-15 14:48:58 +00:00
|
|
|
public function move($productId) {
|
|
|
|
|
$result = Cart::moveToCart($productId);
|
|
|
|
|
|
|
|
|
|
$wishlist = $this->wishlist->findWhere(['customer_id' => auth()->guard('customer')->user()->id, 'product_id' => $productId]);
|
|
|
|
|
|
|
|
|
|
if($this->wishlist->delete($wishlist[0]->id)) {
|
|
|
|
|
session()->flash('success', 'Item Moved To Cart Successfully');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('error', 'Item Cannot Be Moved To Cart Successfully');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
2018-10-18 12:12:41 +00:00
|
|
|
}
|