laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

Transfer shopping cart from guest to user logged!?

Open turkidkil opened this issue 5 years ago • 1 comments

I only have authentication by username without password

All issues explanations have been used here all Did not succeed answers and solutions https://github.com/darryldecode/laravelshoppingcart/issues?q=is%3Aissue+login+

The problem is the sessions When I am a guest, the session code is for example 9ot0CjCYUQpYR10ox43A9GoHVp6vliXuFFsoZlGU

After registration it is renewed to a different code HXTc6LXNEW79QzvrvnKwi2N3IBQWwSroBfghOznR

in config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'customers',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Modules\User\Entities\User::class,
        ],

        'customers' => [
            'driver' => 'eloquent',
            'model' => Modules\User\Entities\Customer::class,
        ],
    ],

CACHE_DRIVER=redis SESSION_DRIVER=redis

in User/Http/Controllers/AuthController.php

public function verify(VerifyCodeRequest $request)
{
    $customer = $this->customer($request);
    /* Check code etc. */

    if ($customer->exists) {
     auth()->login($customer,true);
    }
}

in Cart/Providers/CartServiceProvider.php

    public function register()
    {
        $this->app->singleton(Cart::class, function ($app) {
            return new Cart(
                $app['session'],
                $app['events'],
                'cart',
                session()->getId(),
                config('fleetcart.modules.cart.config')
            );
        }); 
}

Is there any solution ^ـ^

Thanks guys

turkidkil avatar Oct 13 '20 09:10 turkidkil

in Cart/Listeners/TransferGuestCartToUser.php

        $guest_session = session('session_old');
        session()->setId($guest_session);
        session()->start();

and in User/Providers/EventServiceProvider.php

        \Illuminate\Auth\Events\Login::class => [
            \Modules\Cart\Listeners\TransferGuestCartToUser::class
        ] 

It works successfully

But I don't know is this the correct way!

There is only a small problem Maybe not important If the same user is logged in from multiple browsers The cart is different

But I don't know if this will affect later Or cause problems!

turkidkil avatar Oct 13 '20 11:10 turkidkil