new: keep cart when login in
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers\Shop\Auth;
|
namespace App\Http\Controllers\Shop\Auth;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Repositories\Core\User\ShopCart;
|
||||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
@@ -31,6 +32,7 @@ class LoginController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
if ($this->guard()->attempt($credentials, $request->get('remember'))) {
|
if ($this->guard()->attempt($credentials, $request->get('remember'))) {
|
||||||
|
ShopCart::migrateGuestCartToUser();
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
if (back()->getTargetUrl() === route('Shop.Orders.store')) {
|
if (back()->getTargetUrl() === route('Shop.Orders.store')) {
|
||||||
$route = 'Shop.Orders.order';
|
$route = 'Shop.Orders.order';
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Shop\Auth;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\Shop\RegisterCustomer;
|
use App\Http\Requests\Shop\RegisterCustomer;
|
||||||
|
use App\Repositories\Core\User\ShopCart;
|
||||||
use App\Repositories\Shop\CustomerSaleChannels;
|
use App\Repositories\Shop\CustomerSaleChannels;
|
||||||
use App\Repositories\Shop\CustomerAddresses;
|
use App\Repositories\Shop\CustomerAddresses;
|
||||||
use App\Repositories\Shop\Customers;
|
use App\Repositories\Shop\Customers;
|
||||||
@@ -33,6 +34,7 @@ class RegisterController extends Controller
|
|||||||
$user = $this->create($request->all());
|
$user = $this->create($request->all());
|
||||||
|
|
||||||
$this->guard()->login($user);
|
$this->guard()->login($user);
|
||||||
|
ShopCart::migrateGuestCartToUser();
|
||||||
|
|
||||||
return $request->wantsJson()
|
return $request->wantsJson()
|
||||||
? new JsonResponse([], 201)
|
? new JsonResponse([], 201)
|
||||||
|
|||||||
@@ -94,11 +94,106 @@ class ShopCart
|
|||||||
return self::get()->getContent();
|
return self::get()->getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get()
|
public static function migrateGuestCartToUser($userId = null)
|
||||||
{
|
{
|
||||||
$userId = Auth::guard('customer')->id();
|
$userId = self::resolveUserId($userId);
|
||||||
$sessionKey = 'cart_'.sha1(static::class . ($userId ?? 'guest'));
|
|
||||||
|
|
||||||
return Cart::session($sessionKey);
|
if ($userId === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$guestSessionKey = self::sessionKey();
|
||||||
|
$guestItems = Cart::session($guestSessionKey)->getContent();
|
||||||
|
|
||||||
|
if ($guestItems->count() === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$userSessionKey = self::sessionKey($userId);
|
||||||
|
|
||||||
|
foreach ($guestItems as $item) {
|
||||||
|
$existing = Cart::session($userSessionKey)->get($item->id);
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
Cart::session($userSessionKey)->update($item->id, [
|
||||||
|
'quantity' => [
|
||||||
|
'relative' => false,
|
||||||
|
'value' => $existing->quantity + $item->quantity,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$itemData = [
|
||||||
|
'id' => $item->id,
|
||||||
|
'name' => $item->name,
|
||||||
|
'price' => $item->price,
|
||||||
|
'quantity' => $item->quantity,
|
||||||
|
'attributes' => self::extractAttributes($item),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (isset($item->associatedModel)) {
|
||||||
|
$itemData['associatedModel'] = $item->associatedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
$conditions = self::extractConditions($item);
|
||||||
|
if (! empty($conditions)) {
|
||||||
|
$itemData['conditions'] = $conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cart::session($userSessionKey)->add($itemData);
|
||||||
|
}
|
||||||
|
|
||||||
|
Cart::session($guestSessionKey)->clear();
|
||||||
|
Cart::session($userSessionKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function extractAttributes($item)
|
||||||
|
{
|
||||||
|
if (! isset($item->attributes)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_object($item->attributes) && method_exists($item->attributes, 'toArray')) {
|
||||||
|
return $item->attributes->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (array) $item->attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function extractConditions($item)
|
||||||
|
{
|
||||||
|
if (! isset($item->conditions)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_object($item->conditions) && method_exists($item->conditions, 'toArray')) {
|
||||||
|
return $item->conditions->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (array) $item->conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function resolveUserId($userId = null)
|
||||||
|
{
|
||||||
|
return $userId ?? Auth::guard('customer')->id();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function sessionKey($userId = null)
|
||||||
|
{
|
||||||
|
$key = $userId ?? 'guest';
|
||||||
|
|
||||||
|
return 'cart_'.sha1(static::class.$key);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function session($userId = null)
|
||||||
|
{
|
||||||
|
return Cart::session(self::sessionKey($userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get($userId = null)
|
||||||
|
{
|
||||||
|
return self::session(self::resolveUserId($userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user