2018-09-11 11:19:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Cart;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2018-09-13 11:06:17 +00:00
|
|
|
use Webkul\Cart\Repositories\CartRepository;
|
2018-09-20 08:33:28 +00:00
|
|
|
use Webkul\Cart\Repositories\CartItemRepository;
|
2018-09-26 04:21:14 +00:00
|
|
|
use Webkul\Cart\Repositories\CartAddressRepository;
|
2018-09-13 11:06:17 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-09-21 10:17:43 +00:00
|
|
|
use Webkul\Product\Repositories\ProductRepository;
|
2018-09-11 11:19:40 +00:00
|
|
|
use Cookie;
|
|
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
/**
|
2018-09-26 04:21:14 +00:00
|
|
|
* Facade for all the methods to be implemented in Cart.
|
2018-09-13 11:06:17 +00:00
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com>
|
2018-09-26 14:28:32 +00:00
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
2018-09-13 11:06:17 +00:00
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
2018-09-11 11:19:40 +00:00
|
|
|
class Cart {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* CartRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cart;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* CartItemRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cartItem;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* CustomerRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $customer;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* CartAddressRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cartAddress;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* ProductRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $product;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
|
|
|
|
* @param Webkul\Cart\Repositories\CartRepository $cart
|
|
|
|
|
* @param Webkul\Cart\Repositories\CartItemRepository $cartItem
|
|
|
|
|
* @param Webkul\Cart\Repositories\CartAddressRepository $cartAddress
|
|
|
|
|
* @param Webkul\Customer\Repositories\CustomerRepository $customer
|
|
|
|
|
* @param Webkul\Product\Repositories\ProductRepository $product
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
CartRepository $cart,
|
|
|
|
|
CartItemRepository $cartItem,
|
|
|
|
|
CartAddressRepository $cartAddress,
|
|
|
|
|
CustomerRepository $customer,
|
2018-09-26 14:28:32 +00:00
|
|
|
ProductRepository $product)
|
2018-09-26 04:21:14 +00:00
|
|
|
{
|
2018-09-13 11:06:17 +00:00
|
|
|
$this->customer = $customer;
|
|
|
|
|
|
|
|
|
|
$this->cart = $cart;
|
|
|
|
|
|
2018-09-20 08:33:28 +00:00
|
|
|
$this->cartItem = $cartItem;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
$this->cartAddress = $cartAddress;
|
|
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$this->product = $product;
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
/**
|
|
|
|
|
* Method to check if the product is available and its required quantity
|
|
|
|
|
* is available or not in the inventory sources.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $id
|
|
|
|
|
*
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function canCheckOut($id) {
|
|
|
|
|
$cart = $this->cart->findOneByField('id', 144);
|
|
|
|
|
|
|
|
|
|
$items = $cart->items;
|
|
|
|
|
|
|
|
|
|
$allProdQty = array();
|
|
|
|
|
|
|
|
|
|
$allProdQty1 = array();
|
|
|
|
|
|
|
|
|
|
$totalQty = 0;
|
|
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
|
|
|
|
$inventories = $item->product->inventories;
|
|
|
|
|
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
$inventories = $item->product->inventory_sources->where('status', '=', '1');
|
|
|
|
|
|
|
|
|
|
foreach($inventories as $inventory) {
|
|
|
|
|
dump($inventory->status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dd($allProdQty);
|
|
|
|
|
|
|
|
|
|
dd([true, false]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 13:15:49 +00:00
|
|
|
/**
|
2018-09-26 04:21:14 +00:00
|
|
|
* Create new cart instance with the current item added.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2018-09-26 04:21:14 +00:00
|
|
|
* @param integer $id
|
|
|
|
|
* @param array $data
|
2018-09-14 13:15:49 +00:00
|
|
|
*
|
2018-09-26 04:21:14 +00:00
|
|
|
* @return Response
|
2018-09-22 08:31:18 +00:00
|
|
|
*/
|
2018-09-26 04:21:14 +00:00
|
|
|
public function createNewCart($id, $data)
|
|
|
|
|
{
|
2018-09-26 14:28:32 +00:00
|
|
|
$itemData = $this->prepareItemData($id, $data);
|
|
|
|
|
|
|
|
|
|
// dd($itemData);
|
|
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartData['channel_id'] = core()->getCurrentChannel()->id;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
// this will auto set the customer id for the cart instances if customer is authenticated
|
2018-09-21 10:17:43 +00:00
|
|
|
if(auth()->guard('customer')->check()) {
|
2018-09-22 08:31:18 +00:00
|
|
|
$cartData['customer_id'] = auth()->guard('customer')->user()->id;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$cartData['is_guest'] = 1;
|
|
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
$cartData['customer_full_name'] = auth()->guard('customer')->user()->first_name .' '. auth()->guard('customer')->user()->last_name;
|
2018-09-21 10:17:43 +00:00
|
|
|
}
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$cartData['items_count'] = 1;
|
|
|
|
|
|
|
|
|
|
$cartData['items_quantity'] = $data['quantity'];
|
|
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if($cart = $this->cart->create($cartData)) {
|
2018-09-26 14:28:32 +00:00
|
|
|
$itemData['parent']['cart_id'] = $cart->id;
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-24 12:05:09 +00:00
|
|
|
if ($data['is_configurable'] == "true") {
|
2018-09-26 14:28:32 +00:00
|
|
|
//parent product entry
|
|
|
|
|
$itemData['parent']['additional'] = json_encode($data);
|
|
|
|
|
if($parent = $this->cartItem->create($itemData['parent'])) {
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$itemData['child']['parent_id'] = $parent->id;
|
|
|
|
|
if($child = $this->cartItem->create($itemData['child'])) {
|
|
|
|
|
session()->put('cart', $cart);
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
session()->flash('success', 'Item Added To Cart Successfully');
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
} else if($data['is_configurable'] == "false") {
|
|
|
|
|
if($result = $this->cartItem->create($itemData['parent'])) {
|
|
|
|
|
session()->put('cart', $cart);
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
session()->flash('success', 'Item Added To Cart Successfully');
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-09-13 11:50:12 +00:00
|
|
|
}
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
2018-09-26 04:21:14 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
session()->flash('error', 'Some Error Occured');
|
2018-09-14 13:15:49 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-22 08:31:18 +00:00
|
|
|
/**
|
2018-09-26 14:28:32 +00:00
|
|
|
* Prepare the other data for the product to be added.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $id
|
|
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function prepareItemData($id, $data) {
|
|
|
|
|
unset($data['_token']);
|
|
|
|
|
|
|
|
|
|
if(!isset($data['is_configurable']) || !isset($data['product']) ||!isset($data['quantity'])) {
|
|
|
|
|
session()->flash('error', 'Cart System Integrity Violation, Some Required Fields Missing.');
|
|
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
dd('Missing Essential Parameters, Cannot Proceed Further');
|
2018-09-26 14:28:32 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
} else {
|
|
|
|
|
if($data['is_configurable'] == "true") {
|
|
|
|
|
if(!isset($data['super_attribute'])) {
|
|
|
|
|
session()->flash('error', 'Cart System Integrity Violation, Configurable Options Not Found In Request.');
|
|
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
dd('Super Attributes Missing From the Request Parameters.');
|
2018-09-26 14:28:32 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data['sku'] = $this->product->findOneByField('id', $data['product'])->sku;
|
|
|
|
|
|
|
|
|
|
if(isset($data['is_configurable']) && $data['is_configurable'] == "true") {
|
|
|
|
|
$parentData['sku'] = $data['sku'];
|
|
|
|
|
|
|
|
|
|
$parentData['product_id'] = $id;
|
|
|
|
|
|
|
|
|
|
$parentData['quantity'] = $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$parentData['type'] = 'configurable';
|
|
|
|
|
|
|
|
|
|
$parentData['name'] = $this->product->findOneByField('id', $id)->name;
|
|
|
|
|
|
|
|
|
|
$parentData['price'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->price;
|
|
|
|
|
|
|
|
|
|
$parentData['base_price'] = $parentData['price'];
|
|
|
|
|
|
|
|
|
|
$parentData['item_total'] = $parentData['price'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$parentData['base_item_total'] = $parentData['price'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$parentData['weight'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->weight;
|
|
|
|
|
|
|
|
|
|
$parentData['item_weight'] = $parentData['weight'] * $parentData['quantity'];
|
|
|
|
|
|
|
|
|
|
$parentData['base_item_weight'] = $parentData['weight'] * $parentData['quantity'];
|
|
|
|
|
|
|
|
|
|
//child row data
|
|
|
|
|
$childData['product_id'] = $data['selected_configurable_option'];
|
|
|
|
|
|
|
|
|
|
$childData['quantity'] = 1;
|
|
|
|
|
|
|
|
|
|
$childData['sku'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->sku;
|
|
|
|
|
|
|
|
|
|
$childData['type'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->type;
|
|
|
|
|
|
|
|
|
|
$childData['name'] = $this->product->findOneByField('id', $data['selected_configurable_option'])->name;
|
|
|
|
|
|
|
|
|
|
return ['parent' => $parentData, 'child' => $childData];
|
|
|
|
|
} else {
|
|
|
|
|
$data['product_id'] = $id;
|
|
|
|
|
unset($data['product']);
|
|
|
|
|
|
|
|
|
|
$data['type'] = 'simple';
|
|
|
|
|
|
|
|
|
|
$data['name'] = $this->product->findOneByField('id', $id)->name;
|
|
|
|
|
|
|
|
|
|
$data['price'] = $this->product->findOneByField('id', $id)->price;
|
|
|
|
|
|
|
|
|
|
$data['base_price'] = $data['price'];
|
|
|
|
|
|
|
|
|
|
$data['item_total'] = $data['price'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$data['base_item_total'] = $data['price'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$data['weight'] = $this->product->findOneByField('id', $id)->weight;
|
|
|
|
|
|
|
|
|
|
$data['item_weight'] = $data['weight'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$data['base_item_weight'] = $data['weight'] * $data['quantity'];
|
|
|
|
|
|
|
|
|
|
return ['parent' => $data, 'child' => null];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-22 08:31:18 +00:00
|
|
|
/**
|
2018-09-26 04:21:14 +00:00
|
|
|
* Add Items in a cart with some cart and item details.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
|
|
|
|
* @param @id
|
|
|
|
|
* @param $data
|
|
|
|
|
*
|
2018-09-26 04:21:14 +00:00
|
|
|
* @return void
|
2018-09-22 08:31:18 +00:00
|
|
|
*/
|
2018-09-26 04:21:14 +00:00
|
|
|
public function add($id, $data)
|
|
|
|
|
{
|
2018-09-26 14:28:32 +00:00
|
|
|
$itemData = $this->prepareItemData($id, $data);
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if(session()->has('cart')) {
|
|
|
|
|
$cart = session()->get('cart');
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
$cartItems = $cart->items()->get();
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if(isset($cartItems)) {
|
|
|
|
|
foreach($cartItems as $cartItem) {
|
2018-09-26 14:28:32 +00:00
|
|
|
if($data['is_configurable'] == "false") {
|
|
|
|
|
if($cartItem->product_id == $id) {
|
|
|
|
|
$prevQty = $cartItem->quantity;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$newQty = $data['quantity'];
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$cartItem->update(['quantity' => $prevQty + $newQty]);
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
session()->flash('success', "Product Quantity Successfully Updated");
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
} else if($data['is_configurable'] == "true") {
|
|
|
|
|
//check the parent and child records that holds info abt this product.
|
|
|
|
|
if($cartItem->product_id == $data['selected_configurable_option']) {
|
|
|
|
|
$child = $cartItem;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$parentId = $child->parent_id;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$parent = $this->cartItem->findOneByField('id', $parentId);
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$parentPrice = $parent->price;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$prevQty = $parent->quantity;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$newQty = $data['quantity'];
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$parent->update(['quantity' => $prevQty + $newQty, 'item_total' => $parentPrice * ($prevQty + $newQty)]);
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
session()->flash('success', "Product Quantity Successfully Updated");
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-24 12:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
$parent = $cart->items()->create($itemData['parent']);
|
|
|
|
|
|
|
|
|
|
$itemData['child']['parent_id'] = $parent->id;
|
|
|
|
|
|
|
|
|
|
$cart->items()->create($itemData['child']);
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
session()->flash('success', 'Item Successfully Added To Cart');
|
2018-09-26 14:28:32 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-09-19 07:00:24 +00:00
|
|
|
} else {
|
2018-09-21 10:17:43 +00:00
|
|
|
if(isset($cart)) {
|
|
|
|
|
$this->cart->delete($cart->id);
|
|
|
|
|
} else {
|
|
|
|
|
$this->createNewCart($id, $data);
|
|
|
|
|
}
|
2018-09-13 11:06:17 +00:00
|
|
|
}
|
2018-09-19 07:00:24 +00:00
|
|
|
} else {
|
2018-09-21 10:17:43 +00:00
|
|
|
$this->createNewCart($id, $data);
|
2018-09-13 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-09-26 04:21:14 +00:00
|
|
|
* Use detach to remove the current product from cart tables
|
2018-09-13 11:06:17 +00:00
|
|
|
*
|
2018-09-24 12:05:09 +00:00
|
|
|
* @param Integer $id
|
2018-09-13 11:06:17 +00:00
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
2018-09-26 04:21:14 +00:00
|
|
|
public function remove($id)
|
|
|
|
|
{
|
2018-09-13 11:06:17 +00:00
|
|
|
|
|
|
|
|
dd("Removing Item from Cart");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-09-26 04:21:14 +00:00
|
|
|
* This function handles when guest has some of cart products and then logs in.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2018-09-26 04:21:14 +00:00
|
|
|
* @return Response
|
2018-09-22 08:31:18 +00:00
|
|
|
*/
|
2018-09-26 04:21:14 +00:00
|
|
|
public function mergeCart()
|
|
|
|
|
{
|
2018-09-21 10:17:43 +00:00
|
|
|
if(session()->has('cart')) {
|
|
|
|
|
$cart = session()->get('cart');
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartItems = $cart->items;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$customerCart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if(isset($customerCart)) {
|
|
|
|
|
$customerCartItems = $this->cart->items($customerCart['id']);
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if(isset($customerCart)) {
|
|
|
|
|
foreach($customerCartItems as $customerCartItem) {
|
2018-09-27 07:55:34 +00:00
|
|
|
foreach($cartItems as $cartItem) {
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
if($cartItem->type == "simple" && !isset($cartItem->parent_id)) {
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
if($customerCartItem->type == "simple" && !isset($customerCartItem->parent_id)) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
//update the customer cart item details and delete the guest instance
|
|
|
|
|
if($customerCartItem->product_id == $cartItem->productId) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
$prevQty = $cartItem->quantity;
|
|
|
|
|
$newQty = $customerCartItem->quantity;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
$customerCartItem->update([
|
|
|
|
|
'quantity' => $prevQty + $newQty,
|
|
|
|
|
'item_total' => $customerCartItem->price * ($prevQty + $newQty),
|
|
|
|
|
'base_item_total' => $customerCartItem->price * ($prevQty + $newQty),
|
|
|
|
|
'item_total_weight' => $customerCartItem->weight * ($prevQty + $newQty),
|
|
|
|
|
'base_item_total_weight' => $customerCartItem->weight * ($prevQty + $newQty)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if($cartItem->type == "simple" && isset($cartItem->parent_id)) {
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
if($customerCartItem->type == "simple" && isset($customerCartItem->parent_id)) {
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-27 07:55:34 +00:00
|
|
|
}
|
|
|
|
|
// if($customerCartItem->type == "simple" && isset($customerCartItem->parent_id)) {
|
|
|
|
|
// //write the merge case whem the items exists with the customer cart also.
|
|
|
|
|
// $child = $customerCartItem;
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// $parentId = $child->parent_id;
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// $parent = $this->cartItem->findOneByField('id', $parentId);
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// $parentPrice = $parent->price;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// $prevQty = $parent->quantity;
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// foreach ($cartItems as $key => $cartItem) {
|
|
|
|
|
// if ($cartItem->type == "simple" && isset($cartItem->parent_id)) {
|
|
|
|
|
// $newQty = $data['quantity'];
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// $parent->update(['quantity' => $prevQty + $newQty, 'item_total' => $parentPrice * ($prevQty + $newQty)]);
|
|
|
|
|
// } else if($cartItem->type == "simple" && is_null($cartItem->parent_id)){
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// } elseif($customerCartItem->type == "simple" && !isset($customerCartItem->parent_id)) {
|
|
|
|
|
// foreach($cartItems as $key => $cartItem) {
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-27 07:55:34 +00:00
|
|
|
// if($cartItem->product_id == $customerCartItem->product_id) {
|
|
|
|
|
|
|
|
|
|
// $customerItemQuantity = $customerCartItem->quantity;
|
|
|
|
|
|
|
|
|
|
// $cartItemQuantity = $cartItem->quantity;
|
|
|
|
|
|
|
|
|
|
// $customerCartItem->update(['cart_id' => $customerCart->id, 'quantity' => $cartItemQuantity + $customerItemQuantity]);
|
|
|
|
|
|
|
|
|
|
// $this->cartItem->delete($cartItem->id);
|
|
|
|
|
|
|
|
|
|
// $cartItems->forget($key);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2018-09-17 08:27:32 +00:00
|
|
|
}
|
2018-09-15 09:16:16 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
foreach($cartItems as $cartItem) {
|
|
|
|
|
$cartItem->update(['cart_id' => $customerCart->id]);
|
|
|
|
|
}
|
2018-09-15 09:16:16 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$this->cart->delete($cart->id);
|
2018-09-15 09:16:16 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-09-17 08:27:32 +00:00
|
|
|
} else {
|
2018-09-21 10:17:43 +00:00
|
|
|
foreach($cartItems as $cartItem) {
|
|
|
|
|
$this->cart->update(['customer_id' => auth()->guard('customer')->user()->id], $cart->id);
|
|
|
|
|
}
|
2018-09-15 09:16:16 +00:00
|
|
|
|
2018-09-17 08:27:32 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-09-21 10:17:43 +00:00
|
|
|
} else {
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-26 04:21:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getCart()
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = session()->get('cart'))
|
|
|
|
|
return false;
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save customer address
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function saveCustomerAddress($data)
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = $this->getCart())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
$billingAddress = $data['billing'];
|
|
|
|
|
$shippingAddress = $data['shipping'];
|
|
|
|
|
$billingAddress['cart_id'] = $shippingAddress['cart_id'] = $cart->id;
|
|
|
|
|
|
|
|
|
|
if($billingAddressModel = $cart->biling_address) {
|
|
|
|
|
$this->cartAddress->update($billingAddress, $billingAddressModel->id);
|
|
|
|
|
|
|
|
|
|
if($shippingAddress = $cart->shipping_address) {
|
|
|
|
|
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
|
|
|
|
$this->cartAddress->update($billingAddress, $shippingAddress->id);
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->update($shippingAddress, $shippingAddress->id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
|
|
|
|
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping']));
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'billing']));
|
|
|
|
|
|
|
|
|
|
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
|
|
|
|
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'shipping']));
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->create(array_merge($shippingAddress, ['address_type' => 'shipping']));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save shipping method for cart
|
|
|
|
|
*
|
|
|
|
|
* @param string $shippingMethodCode
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function saveShippingMethod($shippingMethodCode)
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = $this->getCart())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach($cart->shipping_rates as $rate) {
|
|
|
|
|
if($rate->method != $shippingMethodCode) {
|
|
|
|
|
$rate->delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|