2018-09-11 11:19:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-09-28 13:28:54 +00:00
|
|
|
namespace Webkul\Checkout;
|
2018-09-11 11:19:40 +00:00
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2018-09-28 13:28:54 +00:00
|
|
|
use Webkul\Checkout\Repositories\CartRepository;
|
|
|
|
|
use Webkul\Checkout\Repositories\CartItemRepository;
|
|
|
|
|
use Webkul\Checkout\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-10-15 10:39:09 +00:00
|
|
|
use Webkul\Tax\Repositories\TaxCategoryRepository;
|
2018-09-28 13:28:54 +00:00
|
|
|
use Webkul\Checkout\Models\CartPayment;
|
2018-09-11 11:19:40 +00:00
|
|
|
|
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-10-15 10:39:09 +00:00
|
|
|
/**
|
|
|
|
|
* TaxCategoryRepository model
|
|
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $taxCategory;
|
|
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2018-10-11 05:39:17 +00:00
|
|
|
* @param Webkul\Checkout\Repositories\CartRepository $cart
|
|
|
|
|
* @param Webkul\Checkout\Repositories\CartItemRepository $cartItem
|
2018-09-28 13:28:54 +00:00
|
|
|
* @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress
|
2018-10-11 05:39:17 +00:00
|
|
|
* @param Webkul\Customer\Repositories\CustomerRepository $customer
|
|
|
|
|
* @param Webkul\Product\Repositories\ProductRepository $product
|
2018-10-15 10:39:09 +00:00
|
|
|
* @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory
|
2018-09-26 04:21:14 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
CartRepository $cart,
|
|
|
|
|
CartItemRepository $cartItem,
|
|
|
|
|
CartAddressRepository $cartAddress,
|
|
|
|
|
CustomerRepository $customer,
|
2018-10-15 10:39:09 +00:00
|
|
|
ProductRepository $product,
|
|
|
|
|
TaxCategoryRepository $taxCategory
|
|
|
|
|
)
|
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-10-15 10:39:09 +00:00
|
|
|
|
|
|
|
|
$this->taxCategory = $taxCategory;
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
/**
|
2018-10-04 10:28:44 +00:00
|
|
|
* Prepare the other data for the product to be success.
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
|
|
|
|
* @param integer $id
|
|
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function prepareItemData($productId, $data)
|
|
|
|
|
{
|
|
|
|
|
$product = $this->product->findOneByField('id', $productId);
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//Check if the product's information is proper or not.
|
2018-10-11 05:39:17 +00:00
|
|
|
if(!isset($data['product']) || !isset($data['quantity'])) {
|
2018-10-04 10:28:44 +00:00
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_fields'));
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return false;
|
2018-09-28 12:55:48 +00:00
|
|
|
} else {
|
|
|
|
|
if($product->type == 'configurable' && !isset($data['super_attribute'])) {
|
2018-10-04 10:28:44 +00:00
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_options'));
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return false;
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
$child = $childData = null;
|
2018-10-26 04:20:43 +00:00
|
|
|
$additional = [];
|
2018-09-28 12:55:48 +00:00
|
|
|
if($product->type == 'configurable') {
|
|
|
|
|
$child = $this->product->findOneByField('id', $data['selected_configurable_option']);
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-10-26 04:20:43 +00:00
|
|
|
$additional = $this->getProductAttributeOptionDetails($child);
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-10-26 04:20:43 +00:00
|
|
|
unset($additional['html']);
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-10-26 04:20:43 +00:00
|
|
|
$additional['request'] = $data;
|
|
|
|
|
$additional['variant_id'] = $data['selected_configurable_option'];
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
$childData = [
|
|
|
|
|
'product_id' => $data['selected_configurable_option'],
|
|
|
|
|
'sku' => $child->sku,
|
2018-10-11 05:39:17 +00:00
|
|
|
'name' => $child->name,
|
|
|
|
|
'type' => 'simple'
|
2018-09-28 12:55:48 +00:00
|
|
|
];
|
|
|
|
|
}
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$price = ($product->type == 'configurable' ? $child->price : $product->price);
|
2018-10-12 07:21:39 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
$parentData = [
|
|
|
|
|
'sku' => $product->sku,
|
|
|
|
|
'product_id' => $productId,
|
|
|
|
|
'quantity' => $data['quantity'],
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'name' => $product->name,
|
2018-10-11 05:39:17 +00:00
|
|
|
'price' => core()->convertPrice($price),
|
2018-10-10 10:26:27 +00:00
|
|
|
'base_price' => $price,
|
2018-10-11 05:39:17 +00:00
|
|
|
'total' => core()->convertPrice($price * $data['quantity']),
|
2018-10-10 10:26:27 +00:00
|
|
|
'base_total' => $price * $data['quantity'],
|
|
|
|
|
'weight' => $weight = ($product->type == 'configurable' ? $child->weight : $product->weight),
|
|
|
|
|
'total_weight' => $weight * $data['quantity'],
|
2018-10-25 10:03:49 +00:00
|
|
|
'base_total_weight' => $weight * $data['quantity'],
|
2018-10-26 04:20:43 +00:00
|
|
|
'additional' => $additional
|
2018-10-10 10:26:27 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return ['parent' => $parentData, 'child' => $childData];
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* Add Items in a cart with some cart and item details.
|
|
|
|
|
*
|
|
|
|
|
* @param integer $id
|
|
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-10-26 12:23:25 +00:00
|
|
|
public function add($id, $data, $prepared = false, $preparedData = []) {
|
2018-10-22 10:40:05 +00:00
|
|
|
if($prepared == false) {
|
2018-10-18 12:12:41 +00:00
|
|
|
$itemData = $this->prepareItemData($id, $data);
|
2018-10-26 04:20:43 +00:00
|
|
|
} else {
|
2018-10-18 12:12:41 +00:00
|
|
|
$itemData = $preparedData;
|
2018-10-22 10:40:05 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-10-12 12:54:10 +00:00
|
|
|
if(!$itemData) {
|
2018-10-12 07:21:39 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($cart = $this->getCart()) {
|
|
|
|
|
$product = $this->product->find($id);
|
|
|
|
|
|
|
|
|
|
$cartItems = $cart->items;
|
|
|
|
|
|
2018-10-22 10:40:05 +00:00
|
|
|
//check the isset conditions as collection empty object will mislead the condition and error handling case.
|
2018-10-12 07:21:39 +00:00
|
|
|
if(isset($cartItems) && $cartItems->count()) {
|
2018-10-11 05:39:17 +00:00
|
|
|
foreach($cartItems as $cartItem) {
|
|
|
|
|
if($product->type == "simple") {
|
|
|
|
|
|
|
|
|
|
if($cartItem->product_id == $id) {
|
|
|
|
|
$prevQty = $cartItem->quantity;
|
|
|
|
|
$newQty = $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$canBe = $this->canAddOrUpdate($cartItem->id, $prevQty + $newQty);
|
|
|
|
|
|
|
|
|
|
if($canBe == false) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return false;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cartItem->update([
|
|
|
|
|
'quantity' => $prevQty + $newQty,
|
|
|
|
|
'total' => core()->convertPrice($cartItem->price * ($prevQty + $newQty)),
|
|
|
|
|
'base_total' => $cartItem->price * ($prevQty + $newQty)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return true;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
} else if($product->type == "configurable") {
|
|
|
|
|
if($cartItem->type == "configurable") {
|
|
|
|
|
$temp = $this->cartItem->findOneByField('parent_id', $cartItem->id);
|
2018-10-22 10:40:05 +00:00
|
|
|
|
|
|
|
|
if($prepared == true) {
|
|
|
|
|
$data['selected_configurable_option'] = $preparedData['parent']['product_id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($temp->product_id == $data['selected_configurable_option']) {
|
|
|
|
|
$child = $temp->child;
|
|
|
|
|
|
|
|
|
|
$parent = $cartItem;
|
|
|
|
|
$parentPrice = $parent->price;
|
|
|
|
|
|
|
|
|
|
$prevQty = $parent->quantity;
|
|
|
|
|
$newQty = $data['quantity'];
|
|
|
|
|
|
|
|
|
|
$canBe = $this->canAddOrUpdate($cartItem->child->id, $prevQty + $newQty);
|
|
|
|
|
|
|
|
|
|
if($canBe == false) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return false;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parent->update([
|
|
|
|
|
'quantity' => $prevQty + $newQty,
|
|
|
|
|
'total' => core()->convertPrice($parentPrice * ($prevQty + $newQty)),
|
|
|
|
|
'base_total' => $parentPrice * ($prevQty + $newQty)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return true;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($product->type == "configurable") {
|
|
|
|
|
$parent = $cart->items()->create($itemData['parent']);
|
|
|
|
|
|
|
|
|
|
$itemData['child']['parent_id'] = $parent->id;
|
|
|
|
|
|
|
|
|
|
$cart->items()->create($itemData['child']);
|
|
|
|
|
} else if($product->type != "configurable"){
|
|
|
|
|
$parent = $cart->items()->create($itemData['parent']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
} else {
|
|
|
|
|
if(isset($cart)) {
|
|
|
|
|
$this->cart->delete($cart->id);
|
|
|
|
|
} else {
|
2018-10-22 10:40:05 +00:00
|
|
|
if($prepared == false) {
|
|
|
|
|
return $this->createNewCart($id, $data);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return $this->createNewCart($id, $data, true, $preparedData);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-10-22 10:40:05 +00:00
|
|
|
// return $this->createNewCart($id, $data);
|
|
|
|
|
if($prepared == false) {
|
|
|
|
|
return $this->createNewCart($id, $data);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return $this->createNewCart($id, $data, true, $preparedData);
|
|
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 13:15:49 +00:00
|
|
|
/**
|
2018-10-04 10:28:44 +00:00
|
|
|
* Create new cart instance with the current item success.
|
2018-09-22 08:31:18 +00:00
|
|
|
*
|
2018-09-26 04:21:14 +00:00
|
|
|
* @param integer $id
|
2018-10-10 10:26:27 +00:00
|
|
|
* @param array $data
|
2018-09-14 13:15:49 +00:00
|
|
|
*
|
2018-10-12 07:21:39 +00:00
|
|
|
* @return Booleans
|
2018-09-22 08:31:18 +00:00
|
|
|
*/
|
2018-10-26 12:23:25 +00:00
|
|
|
public function createNewCart($id, $data, $prepared = false, $preparedData = []) {
|
|
|
|
|
if($prepared == false) {
|
|
|
|
|
if(isset($data['selected_configurable_option'])) {
|
|
|
|
|
$canAdd = $this->canAdd($data['selected_configurable_option'], $data['quantity']);
|
|
|
|
|
} else {
|
|
|
|
|
$canAdd = $this->canAdd($id, $data['quantity']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!$canAdd) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 10:40:05 +00:00
|
|
|
$itemData = $this->prepareItemData($id, $data);
|
2018-10-26 12:23:25 +00:00
|
|
|
} else {
|
2018-10-22 10:40:05 +00:00
|
|
|
$itemData = $preparedData;
|
2018-10-26 12:23:25 +00:00
|
|
|
}
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//if the item data is not valid to be processed it will be returning false
|
|
|
|
|
if($itemData == false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-21 10:17:43 +00:00
|
|
|
$cartData['channel_id'] = core()->getCurrentChannel()->id;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-10-04 14:16:23 +00:00
|
|
|
//auth user details else they will be set when the customer is guest
|
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-27 19:04:56 +00:00
|
|
|
$cartData['is_guest'] = 0;
|
2018-10-04 10:28:44 +00:00
|
|
|
$cartData['customer_first_name'] = auth()->guard('customer')->user()->first_name;
|
|
|
|
|
$cartData['customer_last_name'] = auth()->guard('customer')->user()->last_name;
|
2018-10-10 10:26:27 +00:00
|
|
|
$cartData['customer_email'] = auth()->guard('customer')->user()->email;
|
2018-09-28 12:55:48 +00:00
|
|
|
} else {
|
|
|
|
|
$cartData['is_guest'] = 1;
|
2018-09-21 10:17:43 +00:00
|
|
|
}
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//set the currency column with the respective currency
|
|
|
|
|
$cartData['global_currency_code'] = core()->getBaseCurrencyCode();
|
|
|
|
|
$cartData['base_currency_code'] = core()->getBaseCurrencyCode();
|
2018-10-17 07:21:47 +00:00
|
|
|
$cartData['channel_currency_code'] = core()->getChannelBaseCurrencyCode();
|
|
|
|
|
$cartData['cart_currency_code'] = core()->getCurrentCurrencyCode();
|
2018-10-12 07:21:39 +00:00
|
|
|
//set the cart items and quantity
|
2018-09-26 14:28:32 +00:00
|
|
|
$cartData['items_count'] = 1;
|
2018-10-22 10:40:05 +00:00
|
|
|
|
|
|
|
|
if($prepared == false) {
|
|
|
|
|
$cartData['items_qty'] = $data['quantity'];
|
|
|
|
|
} else {
|
|
|
|
|
$cartData['items_qty'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//create the cart instance in the database
|
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-10-10 10:26:27 +00:00
|
|
|
$product = $this->product->find($id);
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
if ($product->type == "configurable") {
|
2018-10-04 10:28:44 +00:00
|
|
|
//parent item entry
|
2018-10-22 10:40:05 +00:00
|
|
|
if($prepared == false) {
|
|
|
|
|
$itemData['parent']['additional'] = json_encode($data);
|
|
|
|
|
} else {
|
|
|
|
|
$itemData['parent']['additional'] = json_encode($preparedData);
|
|
|
|
|
}
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
if($parent = $this->cartItem->create($itemData['parent'])) {
|
2018-10-04 10:28:44 +00:00
|
|
|
//child item entry
|
2018-09-26 14:28:32 +00:00
|
|
|
$itemData['child']['parent_id'] = $parent->id;
|
2018-09-27 19:04:56 +00:00
|
|
|
$itemData['child']['cart_id'] = $cart->id;
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2018-09-26 14:28:32 +00:00
|
|
|
if($child = $this->cartItem->create($itemData['child'])) {
|
2018-10-10 10:26:27 +00:00
|
|
|
$this->putCart($cart);
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-10-04 10:28:44 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
|
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
return $cart;
|
2018-09-26 14:28:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-10 10:26:27 +00:00
|
|
|
} else if($product->type != "configurable") {
|
|
|
|
|
if($this->cartItem->create($itemData['parent'])) {
|
|
|
|
|
$this->putCart($cart);
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-10-04 10:28:44 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
return $cart;
|
2018-10-04 10:28:44 +00:00
|
|
|
}
|
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-10-04 10:28:44 +00:00
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.item.error_add'));
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-11 05:39:17 +00:00
|
|
|
* This function handles when guest has some of cart products and then logs in.
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
2018-10-11 05:39:17 +00:00
|
|
|
* @return Response
|
2018-09-28 12:55:48 +00:00
|
|
|
*/
|
2018-10-11 05:39:17 +00:00
|
|
|
public function mergeCart()
|
2018-09-28 12:55:48 +00:00
|
|
|
{
|
2018-10-11 05:39:17 +00:00
|
|
|
if(session()->has('cart')) {
|
2018-10-05 11:59:43 +00:00
|
|
|
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCart = session()->get('cart');
|
2018-09-27 10:46:57 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//when the logged in customer is not having any of the cart instance previously and are active.
|
2018-10-11 05:39:17 +00:00
|
|
|
if(!isset($cart)) {
|
2018-10-12 07:21:39 +00:00
|
|
|
$guestCart->update([
|
|
|
|
|
'customer_id' => auth()->guard('customer')->user()->id,
|
|
|
|
|
'is_guest' => 0,
|
|
|
|
|
'customer_first_name' => auth()->guard('customer')->user()->first_name,
|
|
|
|
|
'customer_last_name' => auth()->guard('customer')->user()->last_name,
|
|
|
|
|
'customer_email' => auth()->guard('customer')->user()->email
|
|
|
|
|
]);
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
session()->forget('cart');
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
return true;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$cartItems = $cart->items;
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCartId = $guestCart->id;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCartItems = $this->cart->findOneByField('id', $guestCartId)->items;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
foreach($guestCartItems as $key => $guestCartItem) {
|
2018-09-21 10:17:43 +00:00
|
|
|
foreach($cartItems as $cartItem) {
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($guestCartItem->type == "simple") {
|
|
|
|
|
if($cartItem->product_id == $guestCartItem->product_id) {
|
2018-09-26 14:28:32 +00:00
|
|
|
$prevQty = $cartItem->quantity;
|
2018-10-11 05:39:17 +00:00
|
|
|
|
|
|
|
|
$newQty = $guestCartItem->quantity;
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
$canBe = $this->canAddOrUpdate($cartItem->id, $prevQty + $newQty);
|
|
|
|
|
|
|
|
|
|
if($canBe == false) {
|
2018-10-12 07:21:39 +00:00
|
|
|
$this->cartItem->delete($guestCartItem->id);
|
|
|
|
|
continue;
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-27 19:04:56 +00:00
|
|
|
$cartItem->update([
|
|
|
|
|
'quantity' => $prevQty + $newQty,
|
2018-10-11 05:39:17 +00:00
|
|
|
'total' => core()->convertPrice($cartItem->price * ($prevQty + $newQty)),
|
|
|
|
|
'base_total' => $cartItem->price * ($prevQty + $newQty),
|
|
|
|
|
'total_weight' => $cartItem->weight * ($prevQty + $newQty),
|
|
|
|
|
'base_total_weight' => $cartItem->weight * ($prevQty + $newQty)
|
2018-09-27 19:04:56 +00:00
|
|
|
]);
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCartItems->forget($key);
|
|
|
|
|
$this->cartItem->delete($guestCartItem->id);
|
2018-09-26 14:28:32 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
} else if($guestCartItem->type == "configurable" && $cartItem->type == "configurable") {
|
|
|
|
|
$guestCartItemChild = $guestCartItem->child;
|
2018-09-27 10:07:54 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$cartItemChild = $cartItem->child;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($guestCartItemChild->product_id == $cartItemChild->product_id) {
|
|
|
|
|
$prevQty = $guestCartItem->quantity;
|
|
|
|
|
$newQty = $cartItem->quantity;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$canBe = $this->canAddOrUpdate($cartItem->child->id, $prevQty + $newQty);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($canBe == false) {
|
2018-10-12 07:21:39 +00:00
|
|
|
$this->cartItem->delete($guestCartItem->id);
|
|
|
|
|
continue;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$cartItem->update([
|
|
|
|
|
'quantity' => $prevQty + $newQty,
|
|
|
|
|
'total' => core()->convertPrice($cartItem->price * ($prevQty + $newQty)),
|
|
|
|
|
'base_total' => $cartItem->price * ($prevQty + $newQty),
|
|
|
|
|
'total_weight' => $cartItem->weight * ($prevQty + $newQty),
|
|
|
|
|
'base_total_weight' => $cartItem->weight * ($prevQty + $newQty)
|
|
|
|
|
]);
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCartItems->forget($key);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
//child will be deleted first
|
2018-10-12 07:21:39 +00:00
|
|
|
// $this->cartItem->delete($guestCartItemChild->id);
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
//then parent will also delete the child if any
|
2018-10-11 05:39:17 +00:00
|
|
|
$this->cartItem->delete($guestCartItem->id);
|
2018-09-26 14:28:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-24 12:05:09 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
//now handle the products that are not deleted.
|
|
|
|
|
foreach($guestCartItems as $guestCartItem) {
|
2018-09-27 19:04:56 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
if($guestCartItem->type == "configurable") {
|
|
|
|
|
$guestCartItem->update(['cart_id' => $cart->id]);
|
2018-09-27 19:04:56 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$guestCartItem->child->update(['cart_id' => $cart->id]);
|
|
|
|
|
} else{
|
|
|
|
|
$guestCartItem->update(['cart_id' => $cart->id]);
|
2018-09-27 19:04:56 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
//delete the guest cart instance.
|
|
|
|
|
$this->cart->delete($guestCartId);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
//forget the guest cart instance
|
|
|
|
|
session()->forget('cart');
|
2018-09-26 14:28:32 +00:00
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
$this->collectTotals();
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-09-19 07:00:24 +00:00
|
|
|
} else {
|
2018-10-11 05:39:17 +00:00
|
|
|
return redirect()->back();
|
2018-09-13 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-09-28 12:55:48 +00:00
|
|
|
* Update the cart on
|
|
|
|
|
* cart checkout page
|
|
|
|
|
*/
|
|
|
|
|
public function update($itemIds)
|
|
|
|
|
{
|
2018-10-10 10:26:27 +00:00
|
|
|
if($cart = $this->getCart()) {
|
2018-09-28 12:55:48 +00:00
|
|
|
$items = $cart->items;
|
|
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
|
|
|
|
foreach($itemIds['qty'] as $id => $quantity) {
|
|
|
|
|
if($id == $item->id) {
|
|
|
|
|
if($item->type == "configurable") {
|
|
|
|
|
$canBe = $this->canAddOrUpdate($item->child->id, $quantity);
|
2018-10-25 08:19:23 +00:00
|
|
|
|
|
|
|
|
if($canBe == false) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$item->update([
|
|
|
|
|
'quantity' => $quantity,
|
|
|
|
|
'total' => core()->convertPrice($item->price * ($quantity)),
|
|
|
|
|
'base_total' => $item->price * ($quantity),
|
|
|
|
|
'total_weight' => $item->weight * ($quantity),
|
|
|
|
|
'base_total_weight' => $item->weight * ($quantity)
|
|
|
|
|
]);
|
2018-09-28 12:55:48 +00:00
|
|
|
} else {
|
|
|
|
|
$canBe = $this->canAddOrUpdate($id, $quantity);
|
|
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
if($canBe == false) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
$prevQty = $item->quantity;
|
|
|
|
|
|
|
|
|
|
$item->update([
|
|
|
|
|
'quantity' => $quantity,
|
|
|
|
|
'total' => core()->convertPrice($item->price * ($quantity)),
|
|
|
|
|
'base_total' => $item->price * ($quantity),
|
|
|
|
|
'total_weight' => $item->weight * ($quantity),
|
|
|
|
|
'base_total_weight' => $item->weight * ($quantity)
|
|
|
|
|
]);
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 08:19:23 +00:00
|
|
|
$this->collectTotals();
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
|
|
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
return $cart;
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.integrity.missing_fields'));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the item from the cart
|
2018-09-13 11:06:17 +00:00
|
|
|
*
|
2018-09-27 19:04:56 +00:00
|
|
|
* @return response
|
2018-09-13 11:06:17 +00:00
|
|
|
*/
|
2018-09-28 12:55:48 +00:00
|
|
|
public function removeItem($itemId)
|
2018-09-26 04:21:14 +00:00
|
|
|
{
|
2018-10-10 05:50:47 +00:00
|
|
|
if($cart = $this->getCart()) {
|
2018-10-10 10:26:27 +00:00
|
|
|
$this->cartItem->delete($itemId);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
|
|
|
|
//delete the cart instance if no items are there
|
2018-10-10 10:26:27 +00:00
|
|
|
if($cart->items()->get()->count() == 0) {
|
|
|
|
|
$this->cart->delete($cart->id);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
|
|
|
|
session()->forget('cart');
|
|
|
|
|
|
2018-10-10 10:26:27 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.quantity.success_remove'));
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
2018-09-27 10:07:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +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 canAddOrUpdate($itemId, $quantity)
|
|
|
|
|
{
|
|
|
|
|
if ($quantity < 1) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.warning'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$item = $this->cartItem->findOneByField('id', $itemId);
|
|
|
|
|
|
|
|
|
|
if($item->product->haveSufficientQuantity($quantity)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 12:23:25 +00:00
|
|
|
/**
|
|
|
|
|
* Can Add the product or not will check the quantity for that particular product
|
2018-10-26 13:45:51 +00:00
|
|
|
* before creation of the cart.
|
2018-10-26 12:23:25 +00:00
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function canAdd($id, $qty) {
|
|
|
|
|
$product = $this->product->find($id);
|
|
|
|
|
|
|
|
|
|
if($product->haveSufficientQuantity($qty)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* Save cart
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function putCart($cart)
|
|
|
|
|
{
|
|
|
|
|
if(!auth()->guard('customer')->check()) {
|
|
|
|
|
session()->put('cart', $cart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getCart()
|
|
|
|
|
{
|
|
|
|
|
$cart = null;
|
|
|
|
|
|
|
|
|
|
if(auth()->guard('customer')->check()) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$cart = $this->cart->findOneWhere([
|
|
|
|
|
'customer_id' => auth()->guard('customer')->user()->id,
|
|
|
|
|
'is_active' => 1
|
|
|
|
|
]);
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
} elseif(session()->has('cart')) {
|
|
|
|
|
$cart = $this->cart->find(session()->get('cart')->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $cart && $cart->is_active ? $cart : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart details in array
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function toArray()
|
|
|
|
|
{
|
|
|
|
|
$cart = $this->getCart();
|
|
|
|
|
|
|
|
|
|
$data = $cart->toArray();
|
|
|
|
|
|
|
|
|
|
$data['shipping_address'] = current($data['shipping_address']);
|
|
|
|
|
|
|
|
|
|
$data['billing_address'] = current($data['billing_address']);
|
|
|
|
|
|
|
|
|
|
$data['selected_shipping_rate'] = $cart->selected_shipping_rate->toArray();
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 10:01:25 +00:00
|
|
|
/**
|
2018-10-25 09:56:24 +00:00
|
|
|
* Returns the items details of the configurable and simple products
|
2018-09-27 10:01:25 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
2018-10-25 09:55:10 +00:00
|
|
|
public function getProductAttributeOptionDetails($product)
|
2018-09-27 10:01:25 +00:00
|
|
|
{
|
|
|
|
|
$data = [];
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
$labels = [];
|
|
|
|
|
|
2018-10-25 09:55:10 +00:00
|
|
|
foreach($product->parent->super_attributes as $attribute) {
|
|
|
|
|
$option = $attribute->options()->where('id', $product->{$attribute->code})->first();
|
2018-10-05 11:59:43 +00:00
|
|
|
|
2018-09-27 10:01:25 +00:00
|
|
|
$data['attributes'][$attribute->code] = [
|
|
|
|
|
'attribute_name' => $attribute->name,
|
2018-10-25 09:55:10 +00:00
|
|
|
'option_id' => $option->id,
|
2018-09-27 10:01:25 +00:00
|
|
|
'option_label' => $option->label,
|
|
|
|
|
];
|
2018-10-05 11:59:43 +00:00
|
|
|
|
|
|
|
|
$labels[] = $attribute->name . ' : ' . $option->label;
|
2018-09-27 10:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
$data['html'] = implode(', ', $labels);
|
|
|
|
|
|
2018-09-27 10:01:25 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
if($billingAddressModel = $cart->billing_address) {
|
2018-09-26 04:21:14 +00:00
|
|
|
$this->cartAddress->update($billingAddress, $billingAddressModel->id);
|
|
|
|
|
|
2018-10-26 13:43:01 +00:00
|
|
|
if($shippingAddressModel = $cart->shipping_address) {
|
2018-09-26 04:21:14 +00:00
|
|
|
if(isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
2018-10-26 13:43:01 +00:00
|
|
|
$this->cartAddress->update($billingAddress, $shippingAddressModel->id);
|
2018-09-26 04:21:14 +00:00
|
|
|
} else {
|
2018-10-26 13:43:01 +00:00
|
|
|
$this->cartAddress->update($shippingAddress, $shippingAddressModel->id);
|
2018-09-26 04:21:14 +00:00
|
|
|
}
|
|
|
|
|
} 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-10-26 13:43:01 +00:00
|
|
|
$cart->customer_email = $cart->shipping_address->email;
|
|
|
|
|
$cart->customer_first_name = $cart->shipping_address->first_name;
|
|
|
|
|
$cart->customer_last_name = $cart->shipping_address->last_name;
|
|
|
|
|
$cart->save();
|
2018-10-15 10:39:09 +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;
|
|
|
|
|
|
2018-09-26 10:19:18 +00:00
|
|
|
$cart->shipping_method = $shippingMethodCode;
|
|
|
|
|
$cart->save();
|
|
|
|
|
|
|
|
|
|
// foreach($cart->shipping_rates as $rate) {
|
|
|
|
|
// if($rate->method != $shippingMethodCode) {
|
|
|
|
|
// $rate->delete();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2018-09-26 04:21:14 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-26 10:19:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save payment method for cart
|
|
|
|
|
*
|
|
|
|
|
* @param string $payment
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function savePaymentMethod($payment)
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = $this->getCart())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if($cartPayment = $cart->payment)
|
|
|
|
|
$cartPayment->delete();
|
|
|
|
|
|
|
|
|
|
$cartPayment = new CartPayment;
|
|
|
|
|
|
|
|
|
|
$cartPayment->method = $payment['method'];
|
|
|
|
|
$cartPayment->cart_id = $cart->id;
|
|
|
|
|
$cartPayment->save();
|
|
|
|
|
|
|
|
|
|
return $cartPayment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates cart totals
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function collectTotals()
|
|
|
|
|
{
|
|
|
|
|
if(!$cart = $this->getCart())
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
$this->validateItems();
|
|
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
$this->calculateItemsTax();
|
2018-10-10 10:41:33 +00:00
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
$cart->grand_total = $cart->base_grand_total = 0;
|
|
|
|
|
$cart->sub_total = $cart->base_sub_total = 0;
|
|
|
|
|
$cart->tax_total = $cart->base_tax_total = 0;
|
2018-09-26 10:19:18 +00:00
|
|
|
|
|
|
|
|
foreach ($cart->items()->get() as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$cart->grand_total = (float) $cart->grand_total + $item->total + $item->tax_amount;
|
|
|
|
|
$cart->base_grand_total = (float) $cart->base_grand_total + $item->base_total + $item->base_tax_amount;
|
2018-09-26 10:19:18 +00:00
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
$cart->sub_total = (float) $cart->sub_total + $item->total;
|
|
|
|
|
$cart->base_sub_total = (float) $cart->base_sub_total + $item->base_total;
|
|
|
|
|
|
|
|
|
|
$cart->tax_total = (float) $cart->tax_total + $item->tax_amount;
|
|
|
|
|
$cart->base_tax_total = (float) $cart->base_tax_total + $item->base_tax_amount;
|
2018-09-26 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($shipping = $cart->selected_shipping_rate) {
|
|
|
|
|
$cart->grand_total = (float) $cart->grand_total + $shipping->price;
|
|
|
|
|
$cart->base_grand_total = (float) $cart->base_grand_total + $shipping->base_price;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
$quantities = 0;
|
|
|
|
|
foreach($cart->items as $item) {
|
|
|
|
|
$quantities = $quantities + $item->quantity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cart->items_count = $cart->items->count();
|
|
|
|
|
|
|
|
|
|
$cart->items_qty = $quantities;
|
|
|
|
|
|
2018-09-26 10:19:18 +00:00
|
|
|
$cart->save();
|
|
|
|
|
}
|
2018-09-27 19:04:56 +00:00
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
/**
|
|
|
|
|
* To validate if the product information is changed by admin and the items have
|
|
|
|
|
* been added to the cart before it.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function validateItems() {
|
|
|
|
|
$cart = $this->getCart();
|
|
|
|
|
|
|
|
|
|
if(count($cart->items) == 0) {
|
|
|
|
|
$this->cart->delete($cart->id);
|
|
|
|
|
|
|
|
|
|
return redirect()->route('shop.home.index');
|
|
|
|
|
} else {
|
|
|
|
|
$items = $cart->items;
|
|
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
|
|
|
|
if($item->product->type == 'configurable') {
|
|
|
|
|
if($item->product->sku != $item->sku) {
|
|
|
|
|
$item->update(['sku' => $item->product->sku]);
|
|
|
|
|
|
|
|
|
|
} else if($item->product->name != $item->name) {
|
|
|
|
|
$item->update(['name' => $item->product->name]);
|
|
|
|
|
|
|
|
|
|
} else if($item->child->product->price != $item->price) {
|
|
|
|
|
$item->update([
|
|
|
|
|
'price' => $item->child->product->price,
|
|
|
|
|
'base_price' => $item->child->product->price,
|
|
|
|
|
'total' => core()->convertPrice($item->child->product->price * ($item->quantity)),
|
|
|
|
|
'base_total' => $item->child->product->price * ($item->quantity),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if($item->product->type == 'simple') {
|
|
|
|
|
if($item->product->sku != $item->sku) {
|
|
|
|
|
$item->update(['sku' => $item->product->sku]);
|
|
|
|
|
|
|
|
|
|
} else if($item->product->name != $item->name) {
|
|
|
|
|
$item->update(['name' => $item->product->name]);
|
|
|
|
|
|
|
|
|
|
} else if($item->product->price != $item->price) {
|
|
|
|
|
$item->update([
|
|
|
|
|
'price' => $item->product->price,
|
|
|
|
|
'base_price' => $item->product->price,
|
|
|
|
|
'total' => core()->convertPrice($item->child->product->price * ($item->quantity)),
|
|
|
|
|
'base_total' => $item->child->product->price * ($item->quantity),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 06:12:39 +00:00
|
|
|
/**
|
|
|
|
|
* Calculates cart items tax
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-10-15 10:39:09 +00:00
|
|
|
public function calculateItemsTax()
|
2018-10-11 06:12:39 +00:00
|
|
|
{
|
|
|
|
|
$cart = $this->getCart();
|
|
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
if(!$shippingAddress = $cart->shipping_address)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-10-11 06:12:39 +00:00
|
|
|
foreach ($cart->items()->get() as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$taxCategory = $this->taxCategory->find($item->product->tax_category_id);
|
|
|
|
|
|
|
|
|
|
if(!$taxCategory)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
$taxRates = $taxCategory->tax_rates()->where([
|
|
|
|
|
'state' => $shippingAddress->state,
|
|
|
|
|
'country' => $shippingAddress->country,
|
|
|
|
|
])->orderBy('tax_rate', 'desc')->get();
|
|
|
|
|
|
|
|
|
|
foreach($taxRates as $rate) {
|
|
|
|
|
$haveTaxRate = false;
|
|
|
|
|
|
|
|
|
|
if(!$rate->is_zip) {
|
|
|
|
|
if($rate->zip_code == '*' || $rate->zip_code == $shippingAddress->postcode) {
|
|
|
|
|
$haveTaxRate = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if($shippingAddress->postcode >= $rate->zip_code && $shippingAddress->postcode <= $rate->zip_code) {
|
|
|
|
|
$haveTaxRate = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($haveTaxRate) {
|
|
|
|
|
$item->tax_percent = $rate->tax_rate;
|
|
|
|
|
$item->tax_amount = ($item->total * $rate->tax_rate) / 100;
|
|
|
|
|
$item->base_tax_amount = ($item->base_total * $rate->tax_rate) / 100;
|
|
|
|
|
|
|
|
|
|
$item->save();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-11 06:12:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if cart has any error
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function hasError()
|
|
|
|
|
{
|
|
|
|
|
if(!$this->getCart())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if(!$this->isItemsHaveSufficientQuantity())
|
|
|
|
|
return true;
|
2018-10-09 12:04:25 +00:00
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if all cart items have sufficient quantity.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function isItemsHaveSufficientQuantity()
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getCart()->items as $item) {
|
|
|
|
|
if(!$this->isItemHaveQuantity($item))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if all cart items have sufficient quantity.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function isItemHaveQuantity($item)
|
|
|
|
|
{
|
|
|
|
|
$product = $item->type == 'configurable' ? $item->child->product : $item->product;
|
|
|
|
|
|
|
|
|
|
if(!$product->haveSufficientQuantity($item->quantity))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-09 12:04:25 +00:00
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
/**
|
|
|
|
|
* Deactivates current cart
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function deActivateCart()
|
|
|
|
|
{
|
|
|
|
|
if($cart = $this->getCart()) {
|
2018-10-05 11:59:43 +00:00
|
|
|
$this->cart->update(['is_active' => false], $cart->id);
|
|
|
|
|
|
|
|
|
|
if(session()->has('cart')) {
|
|
|
|
|
session()->forget('cart');
|
|
|
|
|
}
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate order before creation
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function prepareDataForOrder()
|
|
|
|
|
{
|
|
|
|
|
$data = $this->toArray();
|
|
|
|
|
|
|
|
|
|
$finalData = [
|
|
|
|
|
'customer_id' => $data['customer_id'],
|
|
|
|
|
'is_guest' => $data['is_guest'],
|
|
|
|
|
'customer_email' => $data['customer_email'],
|
|
|
|
|
'customer_first_name' => $data['customer_first_name'],
|
|
|
|
|
'customer_last_name' => $data['customer_last_name'],
|
2018-10-15 10:39:09 +00:00
|
|
|
'customer' => auth()->guard('customer')->check() ? auth()->guard('customer')->user() : null,
|
2018-10-05 06:18:58 +00:00
|
|
|
|
|
|
|
|
'shipping_method' => $data['selected_shipping_rate']['method'],
|
2018-10-09 12:02:22 +00:00
|
|
|
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
|
2018-10-05 06:18:58 +00:00
|
|
|
'shipping_description' => $data['selected_shipping_rate']['method_description'],
|
|
|
|
|
'shipping_amount' => $data['selected_shipping_rate']['price'],
|
|
|
|
|
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
|
|
|
|
|
|
|
|
|
|
'total_item_count' => $data['items_count'],
|
|
|
|
|
'total_qty_ordered' => $data['items_qty'],
|
|
|
|
|
'base_currency_code' => $data['base_currency_code'],
|
|
|
|
|
'channel_currency_code' => $data['channel_currency_code'],
|
|
|
|
|
'order_currency_code' => $data['cart_currency_code'],
|
|
|
|
|
'grand_total' => $data['grand_total'],
|
|
|
|
|
'base_grand_total' => $data['base_grand_total'],
|
|
|
|
|
'sub_total' => $data['sub_total'],
|
|
|
|
|
'base_sub_total' => $data['base_sub_total'],
|
2018-10-09 12:02:22 +00:00
|
|
|
'tax_amount' => $data['tax_total'],
|
|
|
|
|
'base_tax_amount' => $data['base_tax_total'],
|
2018-10-05 06:18:58 +00:00
|
|
|
|
|
|
|
|
'shipping_address' => array_except($data['shipping_address'], ['id', 'cart_id']),
|
|
|
|
|
'billing_address' => array_except($data['billing_address'], ['id', 'cart_id']),
|
|
|
|
|
'payment' => array_except($data['payment'], ['id', 'cart_id']),
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
'channel' => core()->getCurrentChannel(),
|
2018-10-05 06:18:58 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach($data['items'] as $item) {
|
|
|
|
|
$finalData['items'][] = $this->prepareDataForOrderItem($item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $finalData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares data for order item
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function prepareDataForOrderItem($data)
|
|
|
|
|
{
|
|
|
|
|
$finalData = [
|
|
|
|
|
'product' => $this->product->find($data['product_id']),
|
|
|
|
|
'sku' => $data['sku'],
|
|
|
|
|
'type' => $data['type'],
|
|
|
|
|
'name' => $data['name'],
|
|
|
|
|
'weight' => $data['weight'],
|
|
|
|
|
'total_weight' => $data['total_weight'],
|
|
|
|
|
'qty_ordered' => $data['quantity'],
|
|
|
|
|
'price' => $data['price'],
|
|
|
|
|
'base_price' => $data['base_price'],
|
|
|
|
|
'total' => $data['total'],
|
|
|
|
|
'base_total' => $data['base_total'],
|
2018-10-09 12:02:22 +00:00
|
|
|
'tax_percent' => $data['tax_percent'],
|
|
|
|
|
'tax_amount' => $data['tax_amount'],
|
|
|
|
|
'base_tax_amount' => $data['base_tax_amount'],
|
2018-10-05 06:18:58 +00:00
|
|
|
'additional' => $data['additional'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if(isset($data['child']) && $data['child']) {
|
|
|
|
|
$finalData['child'] = $this->prepareDataForOrderItem($data['child']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $finalData;
|
|
|
|
|
}
|
2018-10-15 14:48:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move to Cart
|
|
|
|
|
*
|
|
|
|
|
* Move a wishlist item to cart
|
|
|
|
|
*/
|
|
|
|
|
public function moveToCart($productId) {
|
2018-10-18 12:12:41 +00:00
|
|
|
$product = $this->product->find($productId);
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2018-10-22 10:40:05 +00:00
|
|
|
if($product->parent_id == null ||$product->parent_id == 'null') {
|
2018-10-18 12:12:41 +00:00
|
|
|
$data = [
|
|
|
|
|
'product' => $productId,
|
|
|
|
|
'quantity' => 1,
|
|
|
|
|
];
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2018-10-18 12:12:41 +00:00
|
|
|
$result = $this->add($productId, $data);
|
|
|
|
|
|
|
|
|
|
if($result instanceof Collection || $result == true) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-10-15 14:48:58 +00:00
|
|
|
} else {
|
2018-10-18 12:12:41 +00:00
|
|
|
//in case the product added is a configurable product.
|
|
|
|
|
$result = $this->moveConfigurableFromWishlistToCart($product->parent_id, $product->id);
|
|
|
|
|
|
|
|
|
|
if(is_array($result)) {
|
|
|
|
|
$data['quantity'] = 1;
|
|
|
|
|
|
|
|
|
|
$data['selected_configurable_option'] = $product->parent_id;
|
|
|
|
|
|
|
|
|
|
$moved = $this->add($data['selected_configurable_option'], $data, true, $result);
|
|
|
|
|
|
|
|
|
|
if($moved) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-15 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-18 12:12:41 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move a configurable product from wishlist to cart.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function moveConfigurableFromWishlistToCart($configurableproductId, $productId) {
|
2018-10-29 06:09:09 +00:00
|
|
|
// dd('moving configurable');
|
2018-10-22 10:40:05 +00:00
|
|
|
$product = $this->product->find($configurableproductId);
|
2018-10-18 12:12:41 +00:00
|
|
|
|
2018-10-26 12:23:25 +00:00
|
|
|
$canAdd = $this->product->find($productId)->haveSufficientQuantity(1);
|
|
|
|
|
|
|
|
|
|
if(!$canAdd) {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 12:12:41 +00:00
|
|
|
$child = $childData = null;
|
|
|
|
|
if($product->type == 'configurable') {
|
|
|
|
|
$child = $this->product->findOneByField('id', $productId);
|
|
|
|
|
|
|
|
|
|
$childData = [
|
2018-10-22 10:40:05 +00:00
|
|
|
'product_id' => $configurableproductId,
|
2018-10-18 12:12:41 +00:00
|
|
|
'sku' => $child->sku,
|
|
|
|
|
'name' => $child->name,
|
|
|
|
|
'type' => 'simple'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$price = ($product->type == 'configurable' ? $child->price : $product->price);
|
|
|
|
|
|
2018-10-25 10:03:49 +00:00
|
|
|
$productAddtionalData = $this->getProductAttributeOptionDetails($child);
|
|
|
|
|
|
|
|
|
|
unset($productAddtionalData['html']);
|
|
|
|
|
|
|
|
|
|
$additional = [
|
2018-10-26 12:23:25 +00:00
|
|
|
'request' => $childData,
|
|
|
|
|
'variant_id' => $productId,
|
2018-10-25 10:03:49 +00:00
|
|
|
'attributes' => $productAddtionalData
|
|
|
|
|
];
|
|
|
|
|
|
2018-10-18 12:12:41 +00:00
|
|
|
$parentData = [
|
|
|
|
|
'sku' => $product->sku,
|
|
|
|
|
'product_id' => $productId,
|
2018-10-22 10:40:05 +00:00
|
|
|
'quantity' => 1,
|
2018-10-18 12:12:41 +00:00
|
|
|
'type' => $product->type,
|
|
|
|
|
'name' => $product->name,
|
|
|
|
|
'price' => core()->convertPrice($price),
|
|
|
|
|
'base_price' => $price,
|
2018-10-22 10:40:05 +00:00
|
|
|
'total' => core()->convertPrice($price),
|
|
|
|
|
'base_total' => $price,
|
2018-10-18 12:12:41 +00:00
|
|
|
'weight' => $weight = ($product->type == 'configurable' ? $child->weight : $product->weight),
|
2018-10-22 10:40:05 +00:00
|
|
|
'total_weight' => $weight,
|
2018-10-25 10:03:49 +00:00
|
|
|
'base_total_weight' => $weight,
|
2018-10-29 06:09:09 +00:00
|
|
|
'additional' => $additional
|
2018-10-18 12:12:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return ['parent' => $parentData, 'child' => $childData];
|
|
|
|
|
}
|
2018-10-26 07:05:19 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the buy now process for simple as well as configurable products
|
|
|
|
|
*
|
|
|
|
|
* @return response mixed
|
|
|
|
|
*/
|
|
|
|
|
public function proceedForBuyNow($id) {
|
|
|
|
|
$product = $this->product->findOneByField('id', $id);
|
|
|
|
|
|
|
|
|
|
if($product->type == 'configurable') {
|
|
|
|
|
session()->flash('warning', 'Please Select Options Before Buying This Product');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
$this->moveToCart($id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|