sarga/packages/Webkul/Cart/src/Cart.php

222 lines
6.0 KiB
PHP
Raw Normal View History

2018-09-11 11:19:40 +00:00
<?php
namespace Webkul\Cart;
use Carbon\Carbon;
use Webkul\Cart\Repositories\CartRepository;
use Webkul\Cart\Repositories\CartItemRepository;
2018-09-11 11:19:40 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
2018-09-11 11:19:40 +00:00
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-20 10:00:52 +00:00
* Facade for all
* the methods to be
2018-09-20 10:00:52 +00:00
* implemented in Cart.
*
* @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 {
protected $cart; //cart repository instance
protected $cartItem; //cart_item repository instance
protected $customer; //customer repository instance
protected $product; //product repository instance
2018-09-19 07:00:24 +00:00
public function __construct(CartRepository $cart,
CartItemRepository $cartItem,
CustomerRepository $customer,
ProductRepository $product) {
$this->customer = $customer;
$this->cart = $cart;
$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
* instance with the
* current item added.
*
* @param Integer $id
* @param Mixed $data
2018-09-14 13:15:49 +00:00
*
* @return Mixed
*/
public function createNewCart($id, $data) {
2018-09-21 10:17:43 +00:00
$cartData['channel_id'] = core()->getCurrentChannel()->id;
2018-09-21 10:17:43 +00:00
if(auth()->guard('customer')->check()) {
$cartData['customer_id'] = auth()->guard('customer')->user()->id;
2018-09-20 15:12:11 +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)) {
$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
if ($data['is_configurable'] == "true") {
$temp = $data['super_attribute'];
unset($data['super_attribute']);
$data['additional'] = json_encode($temp);
}
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();
}
/**
* Add Items in a
* cart with some
* cart and item
* details.
*
* @param @id
* @param $data
*
* @return Mixed
*/
2018-09-21 10:17:43 +00:00
public function add($id, $data) {
2018-09-21 10:17:43 +00:00
if(session()->has('cart')) {
$cart = session()->get('cart');
$cartItems = $cart->items()->get();
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-21 10:17:43 +00:00
$newQty = $data['quantity'];
2018-09-21 10:17:43 +00:00
$cartItem->update(['quantity' => $prevQty + $newQty]);
2018-09-21 10:17:43 +00:00
session()->flash('success', "Product Quantity Successfully Updated");
2018-09-19 07:00:24 +00:00
return redirect()->back();
}
}
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
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-19 07:00:24 +00:00
} else {
2018-09-21 10:17:43 +00:00
$this->createNewCart($id, $data);
}
}
/**
* use detach to remove the
* current product from cart tables
*
* @param Integer $id
* @return Mixed
*/
public function remove($id) {
dd("Removing Item from Cart");
}
/**
* This function handles
* when guest has some of
* cart products and then
* logs in.
*
* @return Redirect
*/
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-21 10:17:43 +00:00
$cartItems = $cart->items;
2018-09-21 10:17:43 +00:00
$customerCart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
2018-09-21 10:17:43 +00:00
if(isset($customerCart)) {
$customerCartItems = $this->cart->items($customerCart['id']);
2018-09-21 10:17:43 +00:00
if(isset($customerCart)) {
foreach($customerCartItems as $customerCartItem) {
2018-09-21 10:17:43 +00:00
foreach($cartItems as $key => $cartItem) {
if($cartItem->product_id == $customerCartItem->product_id) {
2018-09-21 10:17:43 +00:00
$customerItemQuantity = $customerCartItem->quantity;
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]);
$this->cartItem->delete($cartItem->id);
2018-09-14 13:15:49 +00:00
$cartItems->forget($key);
2018-09-21 10:17:43 +00:00
}
}
}
2018-09-21 10:17:43 +00:00
foreach($cartItems as $cartItem) {
$cartItem->update(['cart_id' => $customerCart->id]);
}
2018-09-21 10:17:43 +00:00
$this->cart->delete($cart->id);
2018-09-21 10:17:43 +00:00
return redirect()->back();
}
} 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);
}
return redirect()->back();
}
2018-09-21 10:17:43 +00:00
} else {
return redirect()->back();
}
}
2018-09-11 11:19:40 +00:00
}