lukepolo / laracart

Laravel Shopping Cart Package
https://laracart.lukepolo.com
MIT License
575 stars 86 forks source link

Give aiblity to use database instead of session #103

Open lukepolo opened 8 years ago

lukepolo commented 8 years ago

Will create a database version for those looking for it.

Cannonb4ll commented 8 years ago

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.

lukepolo commented 8 years ago

That's basically what that does :-)

lukepolo commented 8 years ago

Unless they logout ><

umbertix commented 8 years ago

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 ;)

lukepolo commented 8 years ago

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.

umbertix commented 8 years ago

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.

lukepolo commented 8 years ago

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

lukepolo commented 8 years ago

@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/

daniel-farina commented 8 years ago

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

lukepolo commented 8 years ago

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.

daniel-farina commented 8 years ago

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!

lukepolo commented 8 years ago

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.

daniel-farina commented 8 years ago

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!

lukepolo commented 8 years ago

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

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

lukepolo commented 8 years ago

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

lukepolo commented 8 years ago

@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

Cannonb4ll commented 6 years ago

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

lukepolo commented 6 years ago

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.

ShahidH commented 6 years ago

I am looking forward for this.

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

t-prod commented 6 years ago

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.

lukepolo commented 6 years ago

This should work :

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

t-prod commented 6 years ago

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 commented 6 years ago

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

lukepolo commented 6 years ago

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);
t-prod commented 6 years ago

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