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>
|
|
|
|
|
* @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,
|
|
|
|
|
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-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 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-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()) {
|
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-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-24 12:05:09 +00:00
|
|
|
if ($data['is_configurable'] == "true") {
|
|
|
|
|
$temp = $data['super_attribute'];
|
|
|
|
|
|
|
|
|
|
unset($data['super_attribute']);
|
|
|
|
|
|
|
|
|
|
$data['additional'] = json_encode($temp);
|
|
|
|
|
}
|
|
|
|
|
|
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-26 04:21:14 +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-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-24 12:05:09 +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-24 12:05:09 +00:00
|
|
|
if ($data['is_configurable'] == "true") {
|
|
|
|
|
$temp = $data['super_attribute'];
|
|
|
|
|
|
|
|
|
|
unset($data['super_attribute']);
|
|
|
|
|
|
|
|
|
|
$data['additional'] = json_encode($temp);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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-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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-26 04:21:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getCart()
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = session()->get('cart'))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
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']));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|