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

153 lines
3.5 KiB
PHP
Raw Normal View History

2018-09-06 06:20:30 +00:00
<?php
namespace Webkul\Cart\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
2018-09-20 10:00:52 +00:00
use Webkul\Product\Repositories\ProductRepository;
2018-09-10 09:31:34 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Product\ProductImage;
use Webkul\Product\Product\View as ProductView;
use Cart;
2018-09-06 06:20:30 +00:00
/**
* Cart controller for the customer
* and guest users for adding and
* removing the products in the
* cart.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartController extends Controller
{
/**
* Protected Variables that
* holds instances of the
* repository classes.
2018-09-06 06:20:30 +00:00
*
* @param Array $_config
* @param $cart
* @param $cartItem
* @param $customer
* @param $product
* @param $productImage
* @param $productView
2018-09-06 06:20:30 +00:00
*/
protected $_config;
protected $cart;
protected $cartItem;
2018-09-06 06:20:30 +00:00
2018-09-10 09:31:34 +00:00
protected $customer;
2018-09-06 06:20:30 +00:00
2018-09-20 10:00:52 +00:00
protected $product;
protected $productView;
public function __construct(
CartRepository $cart,
CartItemRepository $cartItem,
CustomerRepository $customer,
ProductRepository $product,
ProductImage $productImage,
2018-09-26 04:21:14 +00:00
ProductView $productView
) {
2018-09-26 04:21:14 +00:00
// $this->middleware('customer')->except(['add', 'remove', 'test']);
2018-09-10 09:31:34 +00:00
$this->customer = $customer;
2018-09-06 06:20:30 +00:00
$this->cart = $cart;
2018-09-10 09:31:34 +00:00
$this->cartItem = $cartItem;
2018-09-20 10:00:52 +00:00
$this->product = $product;
$this->productImage = $productImage;
$this->productView = $productView;
$this->_config = request('_config');
2018-09-10 09:31:34 +00:00
}
2018-09-26 04:21:14 +00:00
/**
* Method to populate
* the cart page which
* will be populated
* before the checkout
* process.
*
* @return Mixed
*/
public function index() {
return view($this->_config['view'])->with('cart', Cart::getCart());
}
2018-09-11 11:19:40 +00:00
/**
* Function for guests
* user to add the product
* in the cart.
*
* @return Mixed
*/
2018-09-10 09:31:34 +00:00
2018-09-11 11:19:40 +00:00
public function add($id) {
// session()->forget('cart');
// return redirect()->back();
$data = request()->input();
2018-09-21 10:17:43 +00:00
Cart::add($id, $data);
2018-09-14 13:15:49 +00:00
return redirect()->back();
2018-09-06 06:20:30 +00:00
}
2018-09-10 09:31:34 +00:00
public function remove($id) {
2018-09-11 11:19:40 +00:00
if(auth()->guard('customer')->check()) {
Cart::remove($id);
} else {
Cart::guestUnitRemove($id);
2018-09-11 11:19:40 +00:00
}
2018-09-14 13:15:49 +00:00
return redirect()->back();
2018-09-11 11:19:40 +00:00
}
public function test() {
$cart = $this->cart->findOneByField('id', 144);
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
dump($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
dd($products);
}
2018-09-27 10:07:54 +00:00
public function mergeTest() {
$cartItems = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id)->items;
$tempId = 15;
foreach($cartItems as $cartItem) {
}
}
2018-09-06 06:20:30 +00:00
}