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;
|
2019-06-28 14:18:52 +00:00
|
|
|
use Webkul\Checkout\Models\CartItem;
|
2018-09-28 13:28:54 +00:00
|
|
|
use Webkul\Checkout\Models\CartPayment;
|
2018-10-30 04:31:01 +00:00
|
|
|
use Webkul\Customer\Repositories\WishlistRepository;
|
2019-02-14 15:50:10 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerAddressRepository;
|
2019-03-05 10:58:55 +00:00
|
|
|
use Webkul\Product\Helpers\Price;
|
2018-09-11 11:19:40 +00:00
|
|
|
|
2018-09-13 11:06:17 +00:00
|
|
|
/**
|
2019-01-29 10:35:30 +00:00
|
|
|
* Facades handler 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
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* CartRepository instance
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cart;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* CartItemRepository instance
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cartItem;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* CustomerRepository instance
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $customer;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* CartAddressRepository instance
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $cartAddress;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* ProductRepository instance
|
2018-09-26 04:21:14 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $product;
|
2018-09-13 11:06:17 +00:00
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* TaxCategoryRepository instance
|
2018-10-15 10:39:09 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $taxCategory;
|
|
|
|
|
|
2018-10-30 04:31:01 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* WishlistRepository instance
|
2018-10-30 04:31:01 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
|
|
|
|
protected $wishlist;
|
|
|
|
|
|
2019-02-14 15:50:10 +00:00
|
|
|
/**
|
2019-06-10 07:49:05 +00:00
|
|
|
* CustomerAddressRepository instance
|
2019-02-14 15:50:10 +00:00
|
|
|
*
|
|
|
|
|
* @var mixed
|
|
|
|
|
*/
|
2019-06-10 07:49:05 +00:00
|
|
|
protected $customerAddress;
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
/**
|
|
|
|
|
* Suppress the session flash messages
|
2019-06-10 07:49:05 +00:00
|
|
|
*/
|
2018-11-16 11:51:10 +00:00
|
|
|
protected $suppressFlash;
|
|
|
|
|
|
2019-03-05 10:58:55 +00:00
|
|
|
/**
|
|
|
|
|
* Product price helper instance
|
2019-06-10 07:49:05 +00:00
|
|
|
*/
|
2019-03-05 10:58:55 +00:00
|
|
|
protected $price;
|
|
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2019-06-10 07:49:05 +00:00
|
|
|
* @param Webkul\Checkout\Repositories\CartRepository $cart
|
|
|
|
|
* @param Webkul\Checkout\Repositories\CartItemRepository $cartItem
|
|
|
|
|
* @param Webkul\Checkout\Repositories\CartAddressRepository $cartAddress
|
|
|
|
|
* @param Webkul\Customer\Repositories\CustomerRepository $customer
|
|
|
|
|
* @param Webkul\Product\Repositories\ProductRepository $product
|
|
|
|
|
* @param Webkul\Product\Repositories\TaxCategoryRepository $taxCategory
|
2019-02-14 15:50:10 +00:00
|
|
|
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
|
2019-06-10 07:49:05 +00:00
|
|
|
* @param Webkul\Product\Repositories\CustomerAddressRepository $customerAddress
|
|
|
|
|
* @param Webkul\Discount\Repositories\CartRuleRepository $cartRule
|
|
|
|
|
* @param Webkul\Helpers\Discount $discount
|
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,
|
2018-10-30 04:31:01 +00:00
|
|
|
TaxCategoryRepository $taxCategory,
|
2019-02-14 15:50:10 +00:00
|
|
|
WishlistRepository $wishlist,
|
2019-03-05 10:58:55 +00:00
|
|
|
CustomerAddressRepository $customerAddress,
|
|
|
|
|
Price $price
|
2018-10-15 10:39:09 +00:00
|
|
|
)
|
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-10-30 04:31:01 +00:00
|
|
|
|
|
|
|
|
$this->wishlist = $wishlist;
|
2018-11-16 11:51:10 +00:00
|
|
|
|
2019-02-14 15:50:10 +00:00
|
|
|
$this->customerAddress = $customerAddress;
|
|
|
|
|
|
2019-03-05 10:58:55 +00:00
|
|
|
$this->price = $price;
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->suppressFlash = false;
|
2018-09-14 13:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-24 10:25:49 +00:00
|
|
|
/**
|
|
|
|
|
* Return current logged in customer
|
|
|
|
|
*
|
|
|
|
|
* @return Customer | Boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getCurrentCustomer()
|
|
|
|
|
{
|
2019-04-24 10:35:29 +00:00
|
|
|
$guard = request()->has('token') ? 'api' : 'customer';
|
2019-04-24 10:25:49 +00:00
|
|
|
|
2019-04-24 10:35:29 +00:00
|
|
|
return auth()->guard($guard);
|
2019-04-24 10:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-28 12:55:48 +00:00
|
|
|
/**
|
2018-11-16 11:51:10 +00:00
|
|
|
* Create new cart instance.
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
|
|
|
|
* @param integer $id
|
2018-11-16 11:51:10 +00:00
|
|
|
* @param array $data
|
2018-09-28 12:55:48 +00:00
|
|
|
*
|
2018-11-16 11:51:10 +00:00
|
|
|
* @return Boolean
|
2018-09-28 12:55:48 +00:00
|
|
|
*/
|
2018-11-16 11:51:10 +00:00
|
|
|
public function create($id, $data, $qty = 1)
|
2018-09-28 12:55:48 +00:00
|
|
|
{
|
2018-11-16 11:51:10 +00:00
|
|
|
$cartData = [
|
|
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
'global_currency_code' => core()->getBaseCurrencyCode(),
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
'base_currency_code' => core()->getBaseCurrencyCode(),
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
'channel_currency_code' => core()->getChannelBaseCurrencyCode(),
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
'cart_currency_code' => core()->getCurrentCurrencyCode(),
|
|
|
|
|
'items_count' => 1
|
|
|
|
|
];
|
2018-10-25 09:56:24 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
//Authentication details
|
2019-04-24 10:25:49 +00:00
|
|
|
if ($this->getCurrentCustomer()->check()) {
|
|
|
|
|
$cartData['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
2018-11-16 11:51:10 +00:00
|
|
|
$cartData['is_guest'] = 0;
|
2019-04-24 10:25:49 +00:00
|
|
|
$cartData['customer_first_name'] = $this->getCurrentCustomer()->user()->first_name;
|
|
|
|
|
$cartData['customer_last_name'] = $this->getCurrentCustomer()->user()->last_name;
|
|
|
|
|
$cartData['customer_email'] = $this->getCurrentCustomer()->user()->email;
|
2018-11-16 11:51:10 +00:00
|
|
|
} else {
|
|
|
|
|
$cartData['is_guest'] = 1;
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$result = $this->cart->create($cartData);
|
2018-10-12 07:21:39 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->putCart($result);
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($result) {
|
|
|
|
|
if ($item = $this->createItem($id, $data))
|
2019-01-07 10:30:28 +00:00
|
|
|
return $item;
|
2018-11-16 11:51:10 +00:00
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.create-error'));
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
2019-06-28 14:18:52 +00:00
|
|
|
public function add($id, $data)
|
|
|
|
|
{
|
2018-11-16 11:51:10 +00:00
|
|
|
$cart = $this->getCart();
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cart != null) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$ifExists = $this->checkIfItemExists($id, $data);
|
2018-10-12 07:21:39 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($ifExists) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$item = $this->cartItem->findOneByField('id', $ifExists);
|
|
|
|
|
|
|
|
|
|
$data['quantity'] = $data['quantity'] + $item->quantity;
|
|
|
|
|
|
|
|
|
|
$result = $this->updateItem($id, $data, $ifExists);
|
2018-10-30 11:22:36 +00:00
|
|
|
} else {
|
2018-11-16 11:51:10 +00:00
|
|
|
$result = $this->createItem($id, $data);
|
2018-10-30 11:22:36 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-30 07:54:54 +00:00
|
|
|
return $result;
|
2018-11-16 11:51:10 +00:00
|
|
|
} else {
|
2018-11-17 06:26:10 +00:00
|
|
|
return $this->create($id, $data);
|
2018-11-16 11:51:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
/**
|
|
|
|
|
* To check if the items exists in the cart or not
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2019-06-28 14:18:52 +00:00
|
|
|
public function checkIfItemExists($id, $data)
|
|
|
|
|
{
|
2018-11-16 11:51:10 +00:00
|
|
|
$items = $this->getCart()->items;
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($items as $item) {
|
|
|
|
|
if ($id == $item->product_id) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOnebyField('id', $id);
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($product->type == 'configurable') {
|
2018-11-16 11:51:10 +00:00
|
|
|
$variant = $this->product->findOneByField('id', $data['selected_configurable_option']);
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($item->child->product_id == $data['selected_configurable_option'])
|
|
|
|
|
return $item->id;
|
|
|
|
|
} else if ($product->type == 'downloadable') {
|
|
|
|
|
// if ($data['links'] == $item->additional['links'])
|
2018-11-16 11:51:10 +00:00
|
|
|
return $item->id;
|
|
|
|
|
} else {
|
|
|
|
|
return $item->id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2018-10-22 10:40:05 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
/**
|
|
|
|
|
* Create the item based on the type of product whether simple or configurable
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed Array $item || Error
|
|
|
|
|
*/
|
|
|
|
|
public function createItem($id, $data)
|
|
|
|
|
{
|
2019-04-24 11:39:46 +00:00
|
|
|
$childProduct = $configurable = false;
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOneByField('id', $id);
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($product->type == 'configurable') {
|
2019-02-15 14:08:10 +00:00
|
|
|
if (! isset($data['selected_configurable_option']) || ! $data['selected_configurable_option']) {
|
2018-12-27 10:52:44 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 11:39:46 +00:00
|
|
|
$childProduct = $this->product->findOneByField('id', $data['selected_configurable_option']);
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if (! $childProduct->haveSufficientQuantity($data['quantity'])) {
|
2019-02-04 08:21:26 +00:00
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($product->isStockable() && ! $product->haveSufficientQuantity($data['quantity'])) {
|
2019-02-04 08:21:26 +00:00
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
//Check if the product's information is proper or not
|
2019-04-24 11:39:46 +00:00
|
|
|
if (! isset($data['product']) || ! isset($data['quantity'])) {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_fields'));
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
|
|
|
|
} else {
|
2019-04-24 11:39:46 +00:00
|
|
|
if ($product->type == 'configurable' && ! isset($data['super_attribute'])) {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('error', trans('shop::app.checkout.cart.integrity.missing_options'));
|
2018-10-30 11:22:36 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
2019-06-28 14:18:52 +00:00
|
|
|
} else if ($product->type == 'downloadable' && (! isset($data['links']) || ! count($data['links']))) {
|
|
|
|
|
throw new \Exception('shop::app.checkout.cart.integrity.missing_links');
|
2018-11-16 11:51:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-30 11:22:36 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$child = $childData = null;
|
|
|
|
|
$additional = [];
|
2019-03-05 10:58:55 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
$price = $this->price->getMinimalPrice($product->type == 'configurable' ? $childProduct : $product);
|
|
|
|
|
|
2019-03-05 10:58:55 +00:00
|
|
|
if ($product->type == 'configurable') {
|
2019-06-28 14:18:52 +00:00
|
|
|
$weight = $childProduct->weight;
|
2019-03-05 10:58:55 +00:00
|
|
|
} else {
|
2019-06-28 14:18:52 +00:00
|
|
|
$weight = $product->isStockable() ? $product->weight : 0;
|
2019-03-05 10:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$parentData = [
|
|
|
|
|
'sku' => $product->sku,
|
|
|
|
|
'quantity' => $data['quantity'],
|
|
|
|
|
'cart_id' => $this->getCart()->id,
|
|
|
|
|
'name' => $product->name,
|
|
|
|
|
'price' => core()->convertPrice($price),
|
|
|
|
|
'base_price' => $price,
|
|
|
|
|
'total' => core()->convertPrice($price * $data['quantity']),
|
|
|
|
|
'base_total' => $price * $data['quantity'],
|
|
|
|
|
'weight' => $weight,
|
|
|
|
|
'total_weight' => $weight * $data['quantity'],
|
|
|
|
|
'base_total_weight' => $weight * $data['quantity'],
|
2019-01-07 11:57:07 +00:00
|
|
|
'additional' => $additional,
|
2019-06-28 14:18:52 +00:00
|
|
|
'type' => $product->type,
|
|
|
|
|
'product_id' => $product->id,
|
2019-01-07 11:57:07 +00:00
|
|
|
'additional' => $data,
|
2018-11-16 11:51:10 +00:00
|
|
|
];
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-04-24 11:39:46 +00:00
|
|
|
if ($product->type == 'configurable') {
|
|
|
|
|
$attributeDetails = $this->getProductAttributeOptionDetails($childProduct);
|
2019-01-07 11:57:07 +00:00
|
|
|
unset($attributeDetails['html']);
|
2018-10-30 11:22:36 +00:00
|
|
|
|
2019-01-07 11:57:07 +00:00
|
|
|
$parentData['additional'] = array_merge($parentData['additional'], $attributeDetails);
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
$childData = [
|
|
|
|
|
'product_id' => (int) $data['selected_configurable_option'],
|
|
|
|
|
'sku' => $childProduct->sku,
|
|
|
|
|
'name' => $childProduct->name,
|
|
|
|
|
'type' => 'simple',
|
|
|
|
|
'cart_id' => $this->getCart()->id
|
|
|
|
|
];
|
|
|
|
|
} else if ($product->type == 'downloadable') {
|
|
|
|
|
$parentData['additional'] = array_merge($parentData['additional'], ['link_lables' => $this->getDownloadableDetails($product)]);
|
2018-11-16 11:51:10 +00:00
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-04-24 11:39:46 +00:00
|
|
|
$item = $this->cartItem->create($parentData);
|
2018-10-22 10:40:05 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if (is_array($childData)) {
|
2019-04-24 11:39:46 +00:00
|
|
|
$childData['parent_id'] = $item->id;
|
2019-05-31 07:18:04 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->cartItem->create($childData);
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
2018-11-16 11:51:10 +00:00
|
|
|
|
2019-04-24 11:39:46 +00:00
|
|
|
return $item;
|
2018-10-11 05:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-14 13:15:49 +00:00
|
|
|
/**
|
2018-11-16 11:51:10 +00:00
|
|
|
* Update the cartItem on cart checkout page and if already added item is added again
|
2019-06-28 14:18:52 +00:00
|
|
|
*
|
2018-11-25 13:43:30 +00:00
|
|
|
* @param $id product_id of cartItem instance
|
|
|
|
|
* @param $data new requested quantities by customer
|
|
|
|
|
* @param $itemId is id from cartItem instance
|
2018-11-16 11:51:10 +00:00
|
|
|
* @return boolean
|
2018-09-22 08:31:18 +00:00
|
|
|
*/
|
2018-11-16 11:51:10 +00:00
|
|
|
public function updateItem($id, $data, $itemId)
|
|
|
|
|
{
|
|
|
|
|
$item = $this->cartItem->findOneByField('id', $itemId);
|
2018-10-26 12:23:25 +00:00
|
|
|
|
2019-01-07 11:57:07 +00:00
|
|
|
if (isset($data['product'])) {
|
|
|
|
|
$additional = $data;
|
|
|
|
|
} else {
|
|
|
|
|
$additional = $item->additional;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($item->type == 'configurable') {
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOneByField('id', $item->child->product_id);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $product->haveSufficientQuantity($data['quantity'])) {
|
2018-10-26 12:23:25 +00:00
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-07 11:57:07 +00:00
|
|
|
|
|
|
|
|
$attributeDetails = $this->getProductAttributeOptionDetails($product);
|
|
|
|
|
unset($attributeDetails['html']);
|
|
|
|
|
|
|
|
|
|
$additional = array_merge($additional, $attributeDetails);
|
2019-06-28 14:18:52 +00:00
|
|
|
} else if ($item->type == 'downloadable') {
|
|
|
|
|
$additional = array_merge($additional, ['link_lables' => $this->getDownloadableDetails($item)]);
|
2018-10-26 12:23:25 +00:00
|
|
|
} else {
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOneByField('id', $item->product_id);
|
2018-10-12 07:21:39 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($product->isStockable() && ! $product->haveSufficientQuantity($data['quantity'])) {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-09-21 10:17:43 +00:00
|
|
|
}
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$quantity = $data['quantity'];
|
2018-10-22 10:40:05 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$result = $item->update([
|
|
|
|
|
'quantity' => $quantity,
|
|
|
|
|
'total' => core()->convertPrice($item->price * ($quantity)),
|
|
|
|
|
'base_total' => $item->price * ($quantity),
|
|
|
|
|
'total_weight' => $item->weight * ($quantity),
|
2019-01-07 11:57:07 +00:00
|
|
|
'base_total_weight' => $item->weight * ($quantity),
|
|
|
|
|
'additional' => $additional
|
2018-11-16 11:51:10 +00:00
|
|
|
]);
|
2018-10-22 10:40:05 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->collectTotals();
|
2018-09-14 13:15:49 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($result) {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
|
2018-09-24 12:05:09 +00:00
|
|
|
|
2019-01-07 11:57:07 +00:00
|
|
|
return $item;
|
2018-11-16 11:51:10 +00:00
|
|
|
} else {
|
|
|
|
|
session()->flash('warning', trans('shop::app.checkout.cart.quantity.error'));
|
2018-10-10 10:26:27 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-04 10:28:44 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
/**
|
|
|
|
|
* Remove the item from the cart
|
|
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
|
|
|
|
public function removeItem($itemId)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cart = $this->getCart()) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->cartItem->delete($itemId);
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
//delete the cart instance if no items are there
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cart->items()->get()->count() == 0) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->cart->delete($cart->id);
|
2018-09-13 13:40:01 +00:00
|
|
|
|
2018-11-28 08:35:44 +00:00
|
|
|
// $this->deActivateCart();
|
2019-01-15 11:54:41 +00:00
|
|
|
if (session()->has('cart')) {
|
2018-11-28 08:35:44 +00:00
|
|
|
session()->forget('cart');
|
|
|
|
|
}
|
2018-09-13 11:50:12 +00:00
|
|
|
}
|
2018-09-26 04:21:14 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.item.success-remove'));
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
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
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (session()->has('cart')) {
|
2019-04-24 10:25:49 +00:00
|
|
|
$cart = $this->cart->findWhere(['customer_id' => $this->getCurrentCustomer()->user()->id, 'is_active' => 1]);
|
2018-12-03 13:07:29 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cart->count()) {
|
2018-12-03 13:07:29 +00:00
|
|
|
$cart = $cart->first();
|
|
|
|
|
} else {
|
|
|
|
|
$cart = false;
|
|
|
|
|
}
|
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.
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart) {
|
2018-10-12 07:21:39 +00:00
|
|
|
$guestCart->update([
|
2019-04-24 10:25:49 +00:00
|
|
|
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
2018-10-12 07:21:39 +00:00
|
|
|
'is_guest' => 0,
|
2019-04-24 10:25:49 +00:00
|
|
|
'customer_first_name' => $this->getCurrentCustomer()->user()->first_name,
|
|
|
|
|
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
|
|
|
|
|
'customer_email' => $this->getCurrentCustomer()->user()->email
|
2018-10-12 07:21:39 +00:00
|
|
|
]);
|
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
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($guestCartItems as $key => $guestCartItem) {
|
|
|
|
|
foreach ($cartItems as $cartItem) {
|
2018-09-20 08:33:28 +00:00
|
|
|
|
2019-01-15 11:54:41 +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-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOneByField('id', $cartItem->product_id);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($product->isStockable() && ! $product->haveSufficientQuantity($prevQty + $newQty)) {
|
2018-10-12 07:21:39 +00:00
|
|
|
$this->cartItem->delete($guestCartItem->id);
|
2019-06-28 14:18:52 +00:00
|
|
|
|
2018-10-12 07:21:39 +00:00
|
|
|
continue;
|
2018-09-28 12:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$data['quantity'] = $newQty + $prevQty;
|
|
|
|
|
|
|
|
|
|
$this->updateItem($cartItem->product_id, $data, $cartItem->id);
|
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
|
|
|
}
|
2019-01-15 11:54:41 +00:00
|
|
|
} else if ($guestCartItem->type == "configurable" && $cartItem->type == "configurable") {
|
2018-10-11 05:39:17 +00:00
|
|
|
$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
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($guestCartItemChild->product_id == $cartItemChild->product_id) {
|
2018-10-11 05:39:17 +00:00
|
|
|
$prevQty = $guestCartItem->quantity;
|
|
|
|
|
$newQty = $cartItem->quantity;
|
2018-09-19 07:00:24 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $this->product->findOneByField('id', $cartItem->child->product_id);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($product->isStockable() && ! $product->haveSufficientQuantity($prevQty + $newQty)) {
|
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-11-16 11:51:10 +00:00
|
|
|
$data['quantity'] = $newQty + $prevQty;
|
2018-09-20 15:12:11 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$this->updateItem($cartItem->product_id, $data, $cartItem->id);
|
2018-09-28 12:55:48 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$guestCartItems->forget($key);
|
2018-09-26 14:28:32 +00:00
|
|
|
|
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-11-16 11:51:10 +00:00
|
|
|
//now handle the products that are not removed from the list of items in the guest cart.
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($guestCartItems as $guestCartItem) {
|
2018-09-27 19:04:56 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($guestCartItem->type == "configurable") {
|
2018-10-11 05:39:17 +00:00
|
|
|
$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();
|
|
|
|
|
|
2018-11-05 04:55:05 +00:00
|
|
|
return true;
|
2018-09-19 07:00:24 +00:00
|
|
|
} else {
|
2018-11-05 04:55:05 +00:00
|
|
|
return true;
|
2018-09-13 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
/**
|
|
|
|
|
* Save cart
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function putCart($cart)
|
|
|
|
|
{
|
2019-04-24 10:25:49 +00:00
|
|
|
if (! $this->getCurrentCustomer()->check()) {
|
2018-10-11 05:39:17 +00:00
|
|
|
session()->put('cart', $cart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getCart()
|
|
|
|
|
{
|
2019-04-16 11:02:30 +00:00
|
|
|
$cart = null;
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-04-24 10:25:49 +00:00
|
|
|
if ($this->getCurrentCustomer()->check()) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$cart = $this->cart->findOneWhere([
|
2019-04-24 10:25:49 +00:00
|
|
|
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
2018-12-27 10:52:44 +00:00
|
|
|
'is_active' => 1
|
|
|
|
|
]);
|
2018-10-15 10:39:09 +00:00
|
|
|
|
2018-12-27 10:52:44 +00:00
|
|
|
} elseif (session()->has('cart')) {
|
2018-10-11 05:39:17 +00:00
|
|
|
$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();
|
|
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
$data['billing_address'] = $cart->billing_address->toArray();
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($cart->haveStockableItems()) {
|
|
|
|
|
$data['shipping_address'] = $cart->shipping_address->toArray();
|
|
|
|
|
|
|
|
|
|
$data['selected_shipping_rate'] = $cart->selected_shipping_rate->toArray();
|
|
|
|
|
}
|
2018-10-11 05:39:17 +00:00
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
$data['payment'] = $cart->payment->toArray();
|
|
|
|
|
|
|
|
|
|
$data['items'] = $cart->items->toArray();
|
|
|
|
|
|
2018-10-11 05:39:17 +00:00
|
|
|
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 = [];
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($product->parent->super_attributes as $attribute) {
|
2018-10-25 09:55:10 +00:00
|
|
|
$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] = [
|
2019-06-28 14:18:52 +00:00
|
|
|
'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
|
|
|
|
2019-06-28 14:18:52 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the items details of the downloadable names
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getDownloadableDetails($item)
|
|
|
|
|
{
|
|
|
|
|
$labels = [];
|
|
|
|
|
|
|
|
|
|
if ($item instanceOf CartItem) {
|
|
|
|
|
$links = $item->additional['links'];
|
|
|
|
|
|
|
|
|
|
$item = $item->product;
|
|
|
|
|
} else {
|
|
|
|
|
$links = request('links');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($item->downloadable_links as $link) {
|
|
|
|
|
if (in_array($link->id, $links))
|
|
|
|
|
$labels[] = $link->title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode(', ', $labels);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 04:21:14 +00:00
|
|
|
/**
|
|
|
|
|
* Save customer address
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
public function saveCustomerAddress($data)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart = $this->getCart())
|
2018-09-26 04:21:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
$billingAddress = $data['billing'];
|
2019-06-28 14:18:52 +00:00
|
|
|
$billingAddress['cart_id'] = $cart->id;
|
2018-09-26 04:21:14 +00:00
|
|
|
|
2019-02-15 14:40:47 +00:00
|
|
|
if (isset($data['billing']['address_id']) && $data['billing']['address_id']) {
|
2019-02-14 15:50:10 +00:00
|
|
|
$address = $this->customerAddress->findOneWhere(['id'=> $data['billing']['address_id']])->toArray();
|
2019-06-28 14:18:52 +00:00
|
|
|
|
2019-04-24 10:25:49 +00:00
|
|
|
$billingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
|
|
|
|
|
$billingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
|
|
|
|
|
$billingAddress['email'] = $this->getCurrentCustomer()->user()->email;
|
2019-02-14 15:50:10 +00:00
|
|
|
$billingAddress['address1'] = $address['address1'];
|
|
|
|
|
$billingAddress['country'] = $address['country'];
|
|
|
|
|
$billingAddress['state'] = $address['state'];
|
|
|
|
|
$billingAddress['city'] = $address['city'];
|
|
|
|
|
$billingAddress['postcode'] = $address['postcode'];
|
|
|
|
|
$billingAddress['phone'] = $address['phone'];
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 14:40:47 +00:00
|
|
|
if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) {
|
2019-04-24 10:25:49 +00:00
|
|
|
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
2019-02-15 14:40:47 +00:00
|
|
|
$this->customerAddress->create($billingAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($cart->haveStockableItems()) {
|
|
|
|
|
$shippingAddress = $data['shipping'];
|
|
|
|
|
$shippingAddress['cart_id'] = $cart->id;
|
|
|
|
|
|
|
|
|
|
if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) {
|
|
|
|
|
$address = $this->customerAddress->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray();
|
|
|
|
|
|
|
|
|
|
$shippingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
|
|
|
|
|
$shippingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
|
|
|
|
|
$shippingAddress['email'] = $this->getCurrentCustomer()->user()->email;
|
|
|
|
|
$shippingAddress['address1'] = $address['address1'];
|
|
|
|
|
$shippingAddress['country'] = $address['country'];
|
|
|
|
|
$shippingAddress['state'] = $address['state'];
|
|
|
|
|
$shippingAddress['city'] = $address['city'];
|
|
|
|
|
$shippingAddress['postcode'] = $address['postcode'];
|
|
|
|
|
$shippingAddress['phone'] = $address['phone'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($data['shipping']['save_as_address']) && $data['shipping']['save_as_address']) {
|
|
|
|
|
$shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
|
|
|
|
|
|
|
|
|
$this->customerAddress->create($shippingAddress);
|
|
|
|
|
}
|
2019-02-15 14:40:47 +00:00
|
|
|
}
|
2019-02-14 15:50:10 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($billingAddressModel = $cart->billing_address) {
|
2018-09-26 04:21:14 +00:00
|
|
|
$this->cartAddress->update($billingAddress, $billingAddressModel->id);
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($cart->haveStockableItems()) {
|
|
|
|
|
if ($shippingAddressModel = $cart->shipping_address) {
|
|
|
|
|
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
|
|
|
|
$this->cartAddress->update($billingAddress, $shippingAddressModel->id);
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->update($shippingAddress, $shippingAddressModel->id);
|
|
|
|
|
}
|
2018-09-26 04:21:14 +00:00
|
|
|
} else {
|
2019-06-28 14:18:52 +00:00
|
|
|
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 04:21:14 +00:00
|
|
|
}
|
2019-06-28 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->cartAddress->create(array_merge($billingAddress, ['address_type' => 'billing']));
|
|
|
|
|
|
|
|
|
|
if ($cart->haveStockableItems()) {
|
2019-01-15 11:54:41 +00:00
|
|
|
if (isset($billingAddress['use_for_shipping']) && $billingAddress['use_for_shipping']) {
|
2018-09-26 04:21:14 +00:00
|
|
|
$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
|
|
|
|
2019-04-24 10:25:49 +00:00
|
|
|
if ($this->getCurrentCustomer()->check()) {
|
|
|
|
|
$cart->customer_email = $this->getCurrentCustomer()->user()->email;
|
|
|
|
|
$cart->customer_first_name = $this->getCurrentCustomer()->user()->first_name;
|
|
|
|
|
$cart->customer_last_name = $this->getCurrentCustomer()->user()->last_name;
|
2018-12-21 13:18:31 +00:00
|
|
|
} else {
|
|
|
|
|
$cart->customer_email = $cart->billing_address->email;
|
|
|
|
|
$cart->customer_first_name = $cart->billing_address->first_name;
|
|
|
|
|
$cart->customer_last_name = $cart->billing_address->last_name;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 13:43:01 +00:00
|
|
|
$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)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart = $this->getCart())
|
2018-09-26 04:21:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
2018-09-26 10:19:18 +00:00
|
|
|
$cart->shipping_method = $shippingMethodCode;
|
|
|
|
|
$cart->save();
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart = $this->getCart())
|
2018-09-26 10:19:18 +00:00
|
|
|
return false;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cartPayment = $cart->payment)
|
2018-09-26 10:19:18 +00:00
|
|
|
$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()
|
|
|
|
|
{
|
2018-11-30 07:40:41 +00:00
|
|
|
$validated = $this->validateItems();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $validated) {
|
2018-09-26 10:19:18 +00:00
|
|
|
return false;
|
2018-11-30 07:40:41 +00:00
|
|
|
}
|
2018-09-26 10:19:18 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart = $this->getCart())
|
2018-11-30 07:40:41 +00:00
|
|
|
return false;
|
2018-10-25 08:19:23 +00:00
|
|
|
|
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;
|
2019-05-23 13:14:16 +00:00
|
|
|
$cart->discount_amount = $cart->base_discount_amount = 0;
|
2018-09-26 10:19:18 +00:00
|
|
|
|
|
|
|
|
foreach ($cart->items()->get() as $item) {
|
2019-05-23 13:14:16 +00:00
|
|
|
$cart->discount_amount += $item->discount_amount;
|
|
|
|
|
$cart->base_discount_amount += $item->base_discount_amount;
|
|
|
|
|
|
|
|
|
|
$cart->grand_total = (float) $cart->grand_total + $item->total + $item->tax_amount - $item->discount_amount;
|
|
|
|
|
$cart->base_grand_total = (float) $cart->base_grand_total + $item->base_total + $item->base_tax_amount - $item->base_discount_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
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($shipping = $cart->selected_shipping_rate) {
|
2018-09-26 10:19:18 +00:00
|
|
|
$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;
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($cart->items as $item) {
|
2018-10-05 11:59:43 +00:00
|
|
|
$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
|
|
|
|
|
*/
|
2018-11-21 11:14:07 +00:00
|
|
|
public function validateItems()
|
|
|
|
|
{
|
2018-10-25 08:19:23 +00:00
|
|
|
$cart = $this->getCart();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $cart) {
|
2018-11-30 07:40:41 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 04:55:05 +00:00
|
|
|
//rare case of accident-->used when there are no items.
|
2019-01-15 11:54:41 +00:00
|
|
|
if (count($cart->items) == 0) {
|
2018-10-25 08:19:23 +00:00
|
|
|
$this->cart->delete($cart->id);
|
|
|
|
|
|
2018-11-30 07:40:41 +00:00
|
|
|
return false;
|
2018-10-25 08:19:23 +00:00
|
|
|
} else {
|
|
|
|
|
$items = $cart->items;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($items as $item) {
|
2019-04-16 10:10:58 +00:00
|
|
|
$productFlat = $item->product_flat;
|
|
|
|
|
|
|
|
|
|
if ($productFlat->type == 'configurable') {
|
|
|
|
|
if ($productFlat->sku != $item->sku) {
|
|
|
|
|
$item->update(['sku' => $productFlat->sku]);
|
2018-10-25 08:19:23 +00:00
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
} else if ($productFlat->name != $item->name) {
|
|
|
|
|
$item->update(['name' => $productFlat->name]);
|
2018-10-25 08:19:23 +00:00
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
} else if ($this->price->getMinimalPrice($item->child->product_flat) != $item->price) {
|
2019-03-05 10:58:55 +00:00
|
|
|
// $price = (float) $item->custom_price ? $item->custom_price : $item->child->product->price;
|
|
|
|
|
|
2019-06-07 05:48:46 +00:00
|
|
|
if (! is_null($item->custom_price)) {
|
2019-03-05 10:58:55 +00:00
|
|
|
$price = $item->custom_price;
|
|
|
|
|
} else {
|
2019-04-16 10:10:58 +00:00
|
|
|
$price = $this->price->getMinimalPrice($item->child->product_flat);
|
2019-03-05 10:58:55 +00:00
|
|
|
}
|
2019-01-07 11:57:07 +00:00
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
$item->update([
|
2019-05-31 07:18:04 +00:00
|
|
|
'price' => core()->convertPrice($price),
|
2019-01-07 11:10:26 +00:00
|
|
|
'base_price' => $price,
|
|
|
|
|
'total' => core()->convertPrice($price * ($item->quantity)),
|
|
|
|
|
'base_total' => $price * ($item->quantity),
|
2018-10-25 08:19:23 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
} else if ($productFlat->type == 'simple') {
|
|
|
|
|
if ($productFlat->sku != $item->sku) {
|
|
|
|
|
$item->update(['sku' => $productFlat->sku]);
|
2018-10-25 08:19:23 +00:00
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
} else if ($productFlat->name != $item->name) {
|
|
|
|
|
$item->update(['name' => $productFlat->name]);
|
2018-10-25 08:19:23 +00:00
|
|
|
|
2019-04-16 10:10:58 +00:00
|
|
|
} else if ($this->price->getMinimalPrice($productFlat) != $item->price) {
|
2019-03-05 10:58:55 +00:00
|
|
|
// $price = (float) $item->custom_price ? $item->custom_price : $item->product->price;
|
|
|
|
|
|
2019-06-07 05:48:46 +00:00
|
|
|
if (! is_null($item->custom_price)) {
|
2019-03-05 10:58:55 +00:00
|
|
|
$price = $item->custom_price;
|
|
|
|
|
} else {
|
2019-04-16 10:10:58 +00:00
|
|
|
$price = $this->price->getMinimalPrice($productFlat);
|
2019-03-05 10:58:55 +00:00
|
|
|
}
|
2019-01-07 11:57:07 +00:00
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
$item->update([
|
2019-05-31 07:18:04 +00:00
|
|
|
'price' => core()->convertPrice($price),
|
2019-01-07 11:10:26 +00:00
|
|
|
'base_price' => $price,
|
|
|
|
|
'total' => core()->convertPrice($price * ($item->quantity)),
|
|
|
|
|
'base_total' => $price * ($item->quantity),
|
2018-10-25 08:19:23 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-14 15:50:10 +00:00
|
|
|
|
2018-10-25 08:19:23 +00:00
|
|
|
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
|
|
|
{
|
2019-02-04 09:48:24 +00:00
|
|
|
if (! $cart = $this->getCart())
|
|
|
|
|
return false;
|
2018-10-11 06:12:39 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $shippingAddress = $cart->shipping_address)
|
2018-10-15 10:39:09 +00:00
|
|
|
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);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $taxCategory)
|
2018-10-15 10:39:09 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
$taxRates = $taxCategory->tax_rates()->where([
|
|
|
|
|
'state' => $shippingAddress->state,
|
|
|
|
|
'country' => $shippingAddress->country,
|
|
|
|
|
])->orderBy('tax_rate', 'desc')->get();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($taxRates as $rate) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$haveTaxRate = false;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $rate->is_zip) {
|
|
|
|
|
if ($rate->zip_code == '*' || $rate->zip_code == $shippingAddress->postcode) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$haveTaxRate = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($shippingAddress->postcode >= $rate->zip_from && $shippingAddress->postcode <= $rate->zip_to) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$haveTaxRate = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($haveTaxRate) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$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
|
2018-11-16 11:51:10 +00:00
|
|
|
*/
|
2018-10-05 11:59:43 +00:00
|
|
|
public function hasError()
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $this->getCart())
|
2018-10-05 11:59:43 +00:00
|
|
|
return true;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $this->isItemsHaveSufficientQuantity())
|
2018-10-05 11:59:43 +00:00
|
|
|
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) {
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $this->isItemHaveQuantity($item))
|
2018-10-05 11:59:43 +00:00
|
|
|
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;
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($product->isStockable() && ! $product->haveSufficientQuantity($item->quantity))
|
2018-10-05 11:59:43 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($cart = $this->getCart()) {
|
2018-10-05 11:59:43 +00:00
|
|
|
$this->cart->update(['is_active' => false], $cart->id);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (session()->has('cart')) {
|
2018-10-05 11:59:43 +00:00
|
|
|
session()->forget('cart');
|
|
|
|
|
}
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate order before creation
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function prepareDataForOrder()
|
|
|
|
|
{
|
|
|
|
|
$data = $this->toArray();
|
|
|
|
|
|
|
|
|
|
$finalData = [
|
2018-11-16 10:11:08 +00:00
|
|
|
'cart_id' => $this->getCart()->id,
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
'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'],
|
2019-04-24 10:25:49 +00:00
|
|
|
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
|
2018-10-05 06:18:58 +00:00
|
|
|
|
|
|
|
|
'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'],
|
2019-06-28 14:18:52 +00:00
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
'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
|
|
|
];
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
if ($this->getCart()->haveStockableItems()) {
|
|
|
|
|
$finalData = array_merge($finalData, [
|
|
|
|
|
'shipping_method' => $data['selected_shipping_rate']['method'],
|
|
|
|
|
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
|
|
|
|
|
'shipping_description' => $data['selected_shipping_rate']['method_description'],
|
|
|
|
|
'shipping_amount' => $data['selected_shipping_rate']['price'],
|
|
|
|
|
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
|
|
|
|
|
'shipping_address' => array_except($data['shipping_address'], ['id', 'cart_id']),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($data['items'] as $item) {
|
2018-10-05 06:18:58 +00:00
|
|
|
$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'],
|
2019-06-13 14:04:13 +00:00
|
|
|
'discount_percent' => $data['discount_percent'],
|
|
|
|
|
'discount_amount' => $data['discount_amount'],
|
|
|
|
|
'base_discount_amount' => $data['base_discount_amount'],
|
2018-10-05 06:18:58 +00:00
|
|
|
'additional' => $data['additional'],
|
|
|
|
|
];
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (isset($data['child']) && $data['child']) {
|
2018-10-05 06:18:58 +00:00
|
|
|
$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
|
|
|
|
|
*/
|
2019-06-28 14:18:52 +00:00
|
|
|
public function moveToCart($wishlistItem) {
|
2018-11-16 11:51:10 +00:00
|
|
|
$product = $wishlistItem->product;
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($product->type == 'simple') {
|
2018-11-16 11:51:10 +00:00
|
|
|
$data['quantity'] = 1;
|
2019-04-16 10:10:58 +00:00
|
|
|
$data['product'] = $product->id;
|
2018-10-15 14:48:58 +00:00
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
$result = $this->add($product->id, $data);
|
2018-10-18 12:12:41 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($result) {
|
2018-11-16 11:51:10 +00:00
|
|
|
return 1;
|
2018-10-18 12:12:41 +00:00
|
|
|
} else {
|
2018-11-16 11:51:10 +00:00
|
|
|
return 0;
|
2018-10-18 12:12:41 +00:00
|
|
|
}
|
2019-01-15 11:54:41 +00:00
|
|
|
} else if ($product->type == 'configurable' && $product->parent_id == null) {
|
2018-11-16 11:51:10 +00:00
|
|
|
return -1;
|
2018-10-15 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-18 12:12:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-11-16 11:51:10 +00:00
|
|
|
* Function to move a already added product to wishlist will run only on customer authentication.
|
2018-10-30 04:31:01 +00:00
|
|
|
*
|
|
|
|
|
* @param instance cartItem $id
|
|
|
|
|
*/
|
2019-06-27 14:21:17 +00:00
|
|
|
public function moveToWishlist($itemId)
|
|
|
|
|
{
|
2018-10-30 11:22:36 +00:00
|
|
|
$cart = $this->getCart();
|
2019-06-28 14:18:52 +00:00
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
$wishlist = [
|
|
|
|
|
'channel_id' => $cart->channel_id,
|
2019-04-24 10:25:49 +00:00
|
|
|
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
2018-10-30 11:22:36 +00:00
|
|
|
];
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
foreach ($cart->items as $item) {
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($item->id == $itemId) {
|
|
|
|
|
if (is_null($item['parent_id']) && $item['type'] == 'simple') {
|
2018-10-30 11:22:36 +00:00
|
|
|
$wishlist['product_id'] = $item->product_id;
|
|
|
|
|
} else {
|
|
|
|
|
$wishlist['product_id'] = $item->child->product_id;
|
2018-11-16 11:51:10 +00:00
|
|
|
$wishtlist['options'] = $item->additional;
|
2018-10-30 11:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 14:18:52 +00:00
|
|
|
$shouldBe = $this->wishlist->findWhere([
|
|
|
|
|
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
|
|
|
|
'product_id' => $wishlist['product_id']
|
|
|
|
|
]);
|
2018-10-30 11:22:36 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($shouldBe->isEmpty()) {
|
2018-10-30 11:22:36 +00:00
|
|
|
$wishlist = $this->wishlist->create($wishlist);
|
|
|
|
|
}
|
2018-10-30 04:31:01 +00:00
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
$result = $this->cartItem->delete($itemId);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($result) {
|
|
|
|
|
if ($cart->items()->count() == 0)
|
2018-10-30 11:22:36 +00:00
|
|
|
$this->cart->delete($cart->id);
|
|
|
|
|
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.move-to-wishlist-success'));
|
2018-10-30 11:22:36 +00:00
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
} else {
|
2018-11-16 11:51:10 +00:00
|
|
|
session()->flash('success', trans('shop::app.checkout.cart.move-to-wishlist-error'));
|
|
|
|
|
|
2018-10-30 11:22:36 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-30 04:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-26 07:05:19 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the buy now process for simple as well as configurable products
|
|
|
|
|
*
|
|
|
|
|
* @return response mixed
|
|
|
|
|
*/
|
2019-06-27 14:21:17 +00:00
|
|
|
public function proceedToBuyNow($id, $quantity)
|
|
|
|
|
{
|
2018-10-26 07:05:19 +00:00
|
|
|
$product = $this->product->findOneByField('id', $id);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($product->type == 'configurable') {
|
2018-10-30 11:22:36 +00:00
|
|
|
session()->flash('warning', trans('shop::app.buynow.no-options'));
|
2018-10-26 07:05:19 +00:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2018-11-16 12:58:41 +00:00
|
|
|
$simpleOrVariant = $this->product->find($id);
|
2018-10-30 11:22:36 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($simpleOrVariant->parent_id != null) {
|
2018-11-17 06:26:10 +00:00
|
|
|
$parent = $simpleOrVariant->parent;
|
|
|
|
|
|
|
|
|
|
$data['product'] = $parent->id;
|
|
|
|
|
$data['selected_configurable_option'] = $simpleOrVariant->id;
|
2019-06-03 10:59:00 +00:00
|
|
|
$data['quantity'] = $quantity;
|
2018-11-17 06:26:10 +00:00
|
|
|
$data['super_attribute'] = 'From Buy Now';
|
|
|
|
|
|
|
|
|
|
$result = $this->add($parent->id, $data);
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
} else {
|
2018-11-30 07:40:41 +00:00
|
|
|
$data['product'] = $id;
|
|
|
|
|
$data['is_configurable'] = false;
|
2019-06-03 10:59:00 +00:00
|
|
|
$data['quantity'] = $quantity;
|
2018-11-30 07:40:41 +00:00
|
|
|
|
2018-11-16 12:58:41 +00:00
|
|
|
$result = $this->add($id, $data);
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2018-10-26 07:05:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-11 11:19:40 +00:00
|
|
|
}
|