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

199 lines
4.9 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-10 09:31:34 +00:00
use Cookie;
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
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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;
2018-09-26 04:21:14 +00:00
public function __construct(
CartRepository $cart,
CartItemRepository $cartItem,
CustomerRepository $customer,
ProductRepository $product,
ProductImage $productImage,
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) {
$data = request()->input();
2018-09-21 10:17:43 +00:00
2018-09-20 10:00:52 +00:00
if(!isset($data['is_configurable']) || !isset($data['product']) ||!isset($data['quantity'])) {
session()->flash('error', 'Cannot Product Due to User\'s miscreancy in system\'s integrity');
return redirect()->back();
}
if($data['is_configurable'] == "false") {
$data['price'] = $this->product->findOneByField('id', $data['product'])->price;
} else {
$id = $data['selected_configurable_option'];
$data['price'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->price;
}
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
}
/**
* This method will return
* the quantities from
* inventory sources whose
* status are not false.
*
* @return Array
*/
public function canAddOrUpdate() {
2018-09-21 14:32:36 +00:00
$cart = $this->cart->findOneByField('id', 144);
2018-09-21 10:17:43 +00:00
$items = $cart->items;
$allProdQty = array();
$allProdQty1 = array();
$totalQty = 0;
foreach($items as $item) {
$inventories = $item->product->inventories;
2018-09-21 14:32:36 +00:00
$inventory_sources = $item->product->inventory_sources;
$totalQty = 0;
foreach($inventory_sources as $inventory_source) {
if($inventory_source->status!=0) {
foreach($inventories as $inventory) {
$totalQty = $totalQty + $inventory->qty;
}
array_push($allProdQty1, $totalQty);
$allProdQty[$item->product->id] = $totalQty;
}
2018-09-21 10:17:43 +00:00
}
2018-09-21 14:32:36 +00:00
}
dd($allProdQty);
2018-09-21 10:17:43 +00:00
2018-09-21 14:32:36 +00:00
foreach ($items as $item) {
$inventories = $item->product->inventory_sources->where('status', '=', '1');
2018-09-21 10:17:43 +00:00
2018-09-21 14:32:36 +00:00
foreach($inventories as $inventory) {
dump($inventory->status);
}
2018-09-21 10:17:43 +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];
}
}
2018-09-21 10:17:43 +00:00
dd($products);
}
2018-09-06 06:20:30 +00:00
}