2018-09-11 11:19:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Cart;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
//Cart repositories
|
|
|
|
|
use Webkul\Cart\Repositories\CartRepository;
|
2018-09-20 08:33:28 +00:00
|
|
|
use Webkul\Cart\Repositories\CartItemRepository;
|
2018-09-11 11:19:40 +00:00
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
//Customer repositories
|
|
|
|
|
use Webkul\Customer\Repositories\CustomerRepository;
|
2018-09-11 11:19:40 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
//Product Repository
|
|
|
|
|
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-20 10:00:52 +00:00
|
|
|
* Facade for all
|
2018-09-13 11:06:17 +00:00
|
|
|
* the methods to be
|
2018-09-20 10:00:52 +00:00
|
|
|
* implemented in Cart.
|
2018-09-13 11:06:17 +00:00
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com>
|
|
|
|
|
* @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-21 13:27:27 +00:00
|
|
|
protected $cart; //item repository instance
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
protected $cartItem; //cart item repository instance
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
protected $customer; //customer repository instance
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
protected $product; //product repository instance
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
public function __construct(CartRepository $cart,
|
|
|
|
|
CartItemRepository $cartItem,
|
|
|
|
|
CustomerRepository $customer,
|
|
|
|
|
ProductRepository $product) {
|
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-21 10:17:43 +00:00
|
|
|
$this->product = $product;
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create New Cart
|
|
|
|
|
* Cart, Cookie &
|
|
|
|
|
* Session.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
2018-09-19 07:00:24 +00:00
|
|
|
*/
|
2018-09-20 08:33:28 +00:00
|
|
|
public function createNewCart($id, $data) {
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartData['channel_id'] = core()->getCurrentChannel()->id;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
if(auth()->guard('customer')->check()) {
|
|
|
|
|
$data['customer_id'] = auth()->guard('customer')->user()->id;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
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-21 10:17:43 +00:00
|
|
|
if($cart = $this->cart->create($cartData)) {
|
2018-09-21 13:27:27 +00:00
|
|
|
$data['cart_id'] = $cart->id;
|
2018-09-21 10:17:43 +00:00
|
|
|
$data['product_id'] = $id;
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
if($result = $this->cartItem->create($data)) {
|
2018-09-21 10:17:43 +00:00
|
|
|
session()->put('cart', $cart);
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-19 07:00:24 +00:00
|
|
|
session()->flash('success', 'Item Added To Cart Successfully');
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-09-13 11:50:12 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
2018-09-21 10:17:43 +00:00
|
|
|
session()->flash('error', 'Some error occured');
|
2018-09-14 13:15:49 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
/*
|
|
|
|
|
handle the after login event for the customers
|
|
|
|
|
when their are pruoducts in the session or cookie
|
|
|
|
|
of the logged in user.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
public function add($id, $data) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
// session()->forget('cart');
|
|
|
|
|
|
|
|
|
|
// return redirect()->back();
|
|
|
|
|
|
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) {
|
|
|
|
|
if($cartItem->product_id == $id) {
|
|
|
|
|
$prevQty = $cartItem->quantity;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$newQty = $data['quantity'];
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartItem->update(['quantity' => $prevQty + $newQty]);
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
session()->flash('success', "Product Quantity Successfully Updated");
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-19 07:00:24 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-09-13 11:06:17 +00:00
|
|
|
}
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$data['cart_id'] = $cart->id;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$data['product_id'] = $id;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cart->items()->create($data);
|
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-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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* use detach to remove the
|
|
|
|
|
* current product from cart tables
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function remove($id) {
|
|
|
|
|
|
|
|
|
|
dd("Removing Item from Cart");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Function to handle merge
|
|
|
|
|
* and sync the cookies products
|
|
|
|
|
* with the existing data of cart
|
|
|
|
|
* in the cart tables;
|
2018-09-15 08:09:56 +00:00
|
|
|
*/
|
2018-09-14 13:15:49 +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-20 08:33:28 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
foreach($cartItems as $key => $cartItem) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
if($cartItem->product_id == $customerCartItem->product_id) {
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$customerItemQuantity = $customerCartItem->quantity;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartItemQuantity = $cartItem->quantity;
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$customerCartItem->update(['cart_id' => $customerCart->id, 'quantity' => $cartItemQuantity + $customerItemQuantity]);
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
$this->cartItem->delete($cartItem->id);
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-09-21 13:27:27 +00:00
|
|
|
$cartItems->forget($key);
|
2018-09-21 10:17:43 +00:00
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroys the session
|
|
|
|
|
* maintained for cart
|
|
|
|
|
* on customer logout.
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function destroyCart() {
|
|
|
|
|
if(session()->has('cart')) {
|
|
|
|
|
session()->forget('cart');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-09-17 08:27:32 +00:00
|
|
|
}
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|
|
|
|
|
}
|