Update Cart in order make compatible for api and session guard
This commit is contained in:
parent
ac03710f07
commit
ee056ee917
|
|
@ -134,6 +134,19 @@ class Cart {
|
|||
$this->suppressFlash = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return current logged in customer
|
||||
*
|
||||
* @return Customer | Boolean
|
||||
*/
|
||||
public function getCurrentCustomer()
|
||||
{
|
||||
$gurad = request()->has('token') ? 'api' : 'customer';
|
||||
|
||||
return auth()->guard($gurad);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new cart instance.
|
||||
*
|
||||
|
|
@ -158,12 +171,12 @@ class Cart {
|
|||
];
|
||||
|
||||
//Authentication details
|
||||
if (auth()->guard('customer')->check()) {
|
||||
$cartData['customer_id'] = auth()->guard('customer')->user()->id;
|
||||
if ($this->getCurrentCustomer()->check()) {
|
||||
$cartData['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
||||
$cartData['is_guest'] = 0;
|
||||
$cartData['customer_first_name'] = auth()->guard('customer')->user()->first_name;
|
||||
$cartData['customer_last_name'] = auth()->guard('customer')->user()->last_name;
|
||||
$cartData['customer_email'] = auth()->guard('customer')->user()->email;
|
||||
$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;
|
||||
} else {
|
||||
$cartData['is_guest'] = 1;
|
||||
}
|
||||
|
|
@ -440,7 +453,7 @@ class Cart {
|
|||
public function mergeCart()
|
||||
{
|
||||
if (session()->has('cart')) {
|
||||
$cart = $this->cart->findWhere(['customer_id' => auth()->guard('customer')->user()->id, 'is_active' => 1]);
|
||||
$cart = $this->cart->findWhere(['customer_id' => $this->getCurrentCustomer()->user()->id, 'is_active' => 1]);
|
||||
|
||||
if ($cart->count()) {
|
||||
$cart = $cart->first();
|
||||
|
|
@ -453,11 +466,11 @@ class Cart {
|
|||
//when the logged in customer is not having any of the cart instance previously and are active.
|
||||
if (! $cart) {
|
||||
$guestCart->update([
|
||||
'customer_id' => auth()->guard('customer')->user()->id,
|
||||
'customer_id' => $this->getCurrentCustomer()->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
|
||||
'customer_first_name' => $this->getCurrentCustomer()->user()->first_name,
|
||||
'customer_last_name' => $this->getCurrentCustomer()->user()->last_name,
|
||||
'customer_email' => $this->getCurrentCustomer()->user()->email
|
||||
]);
|
||||
|
||||
session()->forget('cart');
|
||||
|
|
@ -554,7 +567,7 @@ class Cart {
|
|||
*/
|
||||
public function putCart($cart)
|
||||
{
|
||||
if (! auth()->guard('customer')->check()) {
|
||||
if (! $this->getCurrentCustomer()->check()) {
|
||||
session()->put('cart', $cart);
|
||||
}
|
||||
}
|
||||
|
|
@ -568,9 +581,9 @@ class Cart {
|
|||
{
|
||||
$cart = null;
|
||||
|
||||
if (auth()->guard('customer')->check()) {
|
||||
if ($this->getCurrentCustomer()->check()) {
|
||||
$cart = $this->cart->findOneWhere([
|
||||
'customer_id' => auth()->guard('customer')->user()->id,
|
||||
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
||||
'is_active' => 1
|
||||
]);
|
||||
|
||||
|
|
@ -649,9 +662,9 @@ class Cart {
|
|||
|
||||
if (isset($data['billing']['address_id']) && $data['billing']['address_id']) {
|
||||
$address = $this->customerAddress->findOneWhere(['id'=> $data['billing']['address_id']])->toArray();
|
||||
$billingAddress['first_name'] = auth()->guard('customer')->user()->first_name;
|
||||
$billingAddress['last_name'] = auth()->guard('customer')->user()->last_name;
|
||||
$billingAddress['email'] = auth()->guard('customer')->user()->email;
|
||||
$billingAddress['first_name'] = $this->getCurrentCustomer()->user()->first_name;
|
||||
$billingAddress['last_name'] = $this->getCurrentCustomer()->user()->last_name;
|
||||
$billingAddress['email'] = $this->getCurrentCustomer()->user()->email;
|
||||
$billingAddress['address1'] = $address['address1'];
|
||||
$billingAddress['country'] = $address['country'];
|
||||
$billingAddress['state'] = $address['state'];
|
||||
|
|
@ -662,9 +675,9 @@ class Cart {
|
|||
|
||||
if (isset($data['shipping']['address_id']) && $data['shipping']['address_id']) {
|
||||
$address = $this->customerAddress->findOneWhere(['id'=> $data['shipping']['address_id']])->toArray();
|
||||
$shippingAddress['first_name'] = auth()->guard('customer')->user()->first_name;
|
||||
$shippingAddress['last_name'] = auth()->guard('customer')->user()->last_name;
|
||||
$shippingAddress['email'] = auth()->guard('customer')->user()->email;
|
||||
$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'];
|
||||
|
|
@ -674,12 +687,12 @@ class Cart {
|
|||
}
|
||||
|
||||
if (isset($data['billing']['save_as_address']) && $data['billing']['save_as_address']) {
|
||||
$billingAddress['customer_id'] = auth()->guard('customer')->user()->id;
|
||||
$billingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
||||
$this->customerAddress->create($billingAddress);
|
||||
}
|
||||
|
||||
if (isset($data['shipping']['save_as_address']) && $data['shipping']['save_as_address']) {
|
||||
$shippingAddress['customer_id'] = auth()->guard('customer')->user()->id;
|
||||
$shippingAddress['customer_id'] = $this->getCurrentCustomer()->user()->id;
|
||||
$this->customerAddress->create($shippingAddress);
|
||||
}
|
||||
|
||||
|
|
@ -709,10 +722,10 @@ class Cart {
|
|||
}
|
||||
}
|
||||
|
||||
if (auth()->guard('customer')->check()) {
|
||||
$cart->customer_email = auth()->guard('customer')->user()->email;
|
||||
$cart->customer_first_name = auth()->guard('customer')->user()->first_name;
|
||||
$cart->customer_last_name = auth()->guard('customer')->user()->last_name;
|
||||
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;
|
||||
} else {
|
||||
$cart->customer_email = $cart->billing_address->email;
|
||||
$cart->customer_first_name = $cart->billing_address->first_name;
|
||||
|
|
@ -1021,7 +1034,7 @@ class Cart {
|
|||
'customer_email' => $data['customer_email'],
|
||||
'customer_first_name' => $data['customer_first_name'],
|
||||
'customer_last_name' => $data['customer_last_name'],
|
||||
'customer' => auth()->guard('customer')->check() ? auth()->guard('customer')->user() : null,
|
||||
'customer' => $this->getCurrentCustomer()->check() ? $this->getCurrentCustomer()->user() : null,
|
||||
|
||||
'shipping_method' => $data['selected_shipping_rate']['method'],
|
||||
'shipping_title' => $data['selected_shipping_rate']['carrier_title'] . ' - ' . $data['selected_shipping_rate']['method_title'],
|
||||
|
|
@ -1122,7 +1135,7 @@ class Cart {
|
|||
$wishlist = [];
|
||||
$wishlist = [
|
||||
'channel_id' => $cart->channel_id,
|
||||
'customer_id' => auth()->guard('customer')->user()->id,
|
||||
'customer_id' => $this->getCurrentCustomer()->user()->id,
|
||||
];
|
||||
|
||||
foreach ($items as $item) {
|
||||
|
|
@ -1134,7 +1147,7 @@ class Cart {
|
|||
$wishtlist['options'] = $item->additional;
|
||||
}
|
||||
|
||||
$shouldBe = $this->wishlist->findWhere(['customer_id' => auth()->guard('customer')->user()->id, 'product_id' => $wishlist['product_id']]);
|
||||
$shouldBe = $this->wishlist->findWhere(['customer_id' => $this->getCurrentCustomer()->user()->id, 'product_id' => $wishlist['product_id']]);
|
||||
|
||||
if ($shouldBe->isEmpty()) {
|
||||
$wishlist = $this->wishlist->create($wishlist);
|
||||
|
|
|
|||
|
|
@ -92,7 +92,6 @@ class CartController extends Controller
|
|||
*/
|
||||
public function add($id)
|
||||
{
|
||||
dd(request()->all());
|
||||
try {
|
||||
Event::fire('checkout.cart.add.before', $id);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue