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

175 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Customer\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Repositories\ProductRepository;
2018-10-05 12:00:48 +00:00
use Webkul\Customer\Repositories\WishlistRepository;
use Cart;
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
class WishlistController extends Controller
{
protected $_config;
2018-10-05 12:00:48 +00:00
protected $customer;
2018-10-05 12:00:48 +00:00
protected $wishlist;
protected $product;
2018-10-05 12:00:48 +00:00
/**
* Initializes the required repository instances.
*
* @param $customer
* @param $wishlist
*/
public function __construct(CustomerRepository $customer, WishlistRepository $wishlist, ProductRepository $product)
{
$this->middleware('customer');
$this->_config = request('_config');
$this->customer = $customer;
2018-10-05 12:00:48 +00:00
$this->wishlist = $wishlist;
$this->product = $product;
}
/**
2018-10-05 12:00:48 +00:00
* Displays the listing resources if the customer having items in wishlist.
*/
public function index() {
$wishlistItems = $this->wishlist->findWhere([
'channel_id' => core()->getCurrentChannel()->id,
'customer_id' => auth()->guard('customer')->user()->id]
);
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
*/
public function add($itemId) {
$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.
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();
}
}
/**
* Function to remove item to the wishlist.
*
* @param integer $itemId
*/
public function remove($itemId) {
$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
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();
}
}
/**
* Function to move item from wishlist to cart.
*
* @param integer $itemId
*/
public function move($itemId) {
$wishlistItem = $this->wishlist->findOneByField('id', $itemId);
$result = Cart::moveToCart($wishlistItem);
if($result == 1) {
if($wishlistItem->delete()) {
session()->flash('success', trans('shop::app.wishlist.moved'));
Cart::collectTotals();
return redirect()->back();
} else {
session()->flash('error', trans('shop::app.wishlist.move-error'));
return redirect()->back();
}
} else if($result == 0) {
Session('error', trans('shop::app.wishlist.error'));
return redirect()->back();
} else if($result == -1) {
if(!$wishlistItem->delete()) {
session()->flash('error', trans('shop::app.wishlist.move-error'));
return redirect()->back();
}
session()->flash('info', trans('shop::app.checkout.cart.add-config-warning'));
return redirect()->route('shop.products.index', $wishlistItem->product->url_key);
}
}
/**
* 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();
}
}