laracart icon indicating copy to clipboard operation
laracart copied to clipboard

Give aiblity to use database instead of session

Open lukepolo opened this issue 8 years ago • 25 comments

Will create a database version for those looking for it.

lukepolo avatar Mar 15 '16 15:03 lukepolo

Is this what that migration was for? I see there's a migration with cart_session_id.

A nice feature would be following;

When a user is logged in (that exists in de database), and adds an item to his/her cart, and then leaves it would be saved to their account, so whenever they log back in, they have their cart filled up again fromout the data that has been saved.

Cannonb4ll avatar May 20 '16 12:05 Cannonb4ll

That's basically what that does :-)

lukepolo avatar May 20 '16 12:05 lukepolo

Unless they logout ><

lukepolo avatar May 20 '16 12:05 lukepolo

I think that it would be nice to have the option to persist the session in the DB aswell. So if a user logs out and logs in to have the option to have that information in the DB aswell ;)

umbertix avatar May 20 '16 13:05 umbertix

I dont think it would be to difficult, currently its loading it from the session data, we could serialize up the cart and produce the same results. Now for the proper way of doing it. Ill need to spend time actually designing the database migrations. Im currently working on a new project so it may be a couple of weeks before I can do this.

lukepolo avatar May 20 '16 13:05 lukepolo

It would be a nice to, if you need an extra pair of hands I might be able to help you a bit. But don't worry about the timing at all. The library is already really good as it is.

umbertix avatar May 20 '16 13:05 umbertix

Yah wouldn't mind some help on this if your up to the challenge.

lukepolo avatar May 20 '16 13:05 lukepolo

@umbertix , can you either add me on skype or something ? Would like see how your thinking of doing this , feel free to add me : luke.a.policinski

If your on another timezone (im on est) Im willing to work around that as well.

And on that note have you seen the new docs ? http://laracart.lukepolo.com/

lukepolo avatar May 26 '16 21:05 lukepolo

Nice jobs with the new docs, this library is great. I'm also interested in persistent carts. I'll play with it this week.

daniel-farina avatar Aug 30 '16 23:08 daniel-farina

Take a look at the 2.0 branch, I think I started the database version , been swamped with a new project.

2.0 is highly not done though.

lukepolo avatar Aug 30 '16 23:08 lukepolo

Nice, It's looking good. I will try to get into it and contribute soon.

Quick question related to the current method where the cart is stored in the session, is that information saved on the tmp folder in Linux? so if apache fails or the tmp folder is emptied all shopping carts would be lost?

How reliable using the session to store data is ?

Thanks!

daniel-farina avatar Aug 31 '16 12:08 daniel-farina

So, what ever session your using as your default for laravel is being used to store that data.

For the reliability take a look at your session expires time and that is how reliable that is.

lukepolo avatar Aug 31 '16 13:08 lukepolo

Ok, Now I understand. I just took a look at config/session.php

So it makes me feel better that this is currently saved by laravel using a file system. I could change the lifetime as a quick fix.

A better solution for now I think is serializing the cart content and saving that to the database and then unserialize it.

So I could save the contents of the cart by doing following: $mycart = serialize(LaraCart::getItems()->toArray());

and then unserialize it and convert it back to a object (not sure how to do that yet, need to google it).

Thanks!

daniel-farina avatar Aug 31 '16 14:08 daniel-farina

Sorry, I must have missed documenting something that is very useful here

https://github.com/lukepolo/laracart/issues/103#issuecomment-379557192

lukepolo avatar Aug 31 '16 15:08 lukepolo

when you unserialize it , it will be an instance of Laracart ready for you to use.

lukepolo avatar Aug 31 '16 15:08 lukepolo

@daniel-farina Only one big issue with this you will need to set it to the session manually, (if you want add it in and Ill accept it as a PR)

Read :

https://github.com/lukepolo/laracart/issues/103#issuecomment-379557192

lukepolo avatar Aug 31 '16 15:08 lukepolo

Have you been able to make any progress on this? I was curious :)

Cannonb4ll avatar Nov 06 '17 09:11 Cannonb4ll

I have not :-(. I've been working on too many projects as of late to finish this. I've started a 2.0 branch a year ago that could be a good starting point.

lukepolo avatar Nov 08 '17 14:11 lukepolo

I am looking forward for this.

I think this is a good case for Abandoned Checkouts functionality.

ShahidH avatar Jan 17 '18 11:01 ShahidH

I try the serialize and unserialize to use LaraCart object but it's not working. How can I load it as a LaraCart object ?

You say : $this->session->set($this->prefix.'.'.$this->cart->instance, $this->cart);

But I have the entire object in a variable.

t-prod avatar Apr 06 '18 10:04 t-prod

This should work :

Below : https://github.com/lukepolo/laracart/issues/103#issuecomment-379558802

lukepolo avatar Apr 06 '18 13:04 lukepolo

Hi, thanks for your solution I try this :

session()->put(config('laracart.cache_prefix', 'laracart').'.default', json_decode($cart));

When I call Laracart via :

dd(LaraCart::getItems());

The result is an empty array. I think I must load the session in LaraCart object but how can I do ?

t-prod avatar Apr 08 '18 14:04 t-prod

I try a json_encode and json_decode on the object cause serialize is not working event with SuperClosure package.

t-prod avatar Apr 08 '18 14:04 t-prod

Ok got it to work :

        $userCart =UserCart::firstOrNew([
            'user_id' => \Auth::user()->id
        ]);

        $userCart->fill([
            'cart' => serialize(LaraCart::get()->cart)
        ]);

        $userCart->save();
        $cart = unserialize(\Auth::user()->cart->cart);
        session()->put(config('laracart.cache_prefix').'.'.$cart->instance, $cart);

lukepolo avatar Apr 08 '18 15:04 lukepolo

It's working perfectly, the key was the cart attribute of the returned method LaraCart::get(). Thanks for your help and your time !

t-prod avatar Apr 08 '18 20:04 t-prod