Support the ongoing development of Laravel.io →
Session Database Validation
Last updated 1 year ago.
0

I update the inventory after the user buys the product.

Before he/she clicks on purchase, I recheck the quantity and inform user if item not available.

Last updated 1 year ago.
0

Thanks frocco. I tried this option, but usually, users think that when they add a product to the cart, it is booked.

Does someone know apc? Maybe i can store all the products add by all the users in one cache variable, no?

Last updated 1 year ago.
0

I would be interested to know what solution you find.

Regards

Last updated 1 year ago.
0

What if the item in the cart just sits in the cart? and for how long will it sit in the cart? I think this really boils down too the user story... if you want it to hold the inventory when added to cart, you could do that but would need to release it at some point if the person never checks out.

I would suggest either doing the check when they reach the actual cart and again upon checkout or at least on checkout as you previously mentioned. Then you can see if it truly becomes an issue for your users. If it does, you can then go back and start to build out that logic.

The big win will be making sure you are letting your user know what is happening by alerting them, flash message etc...

Last updated 1 year ago.
0

I believe that this would complicate your application with very minimal profit. As there would be issues that the session was not destroyed ( or the "cart" in the database was not emptied ) and in that case, you may put a time constraint as what @michaeldeol is suggesting.

michaeldeol said:

See if it truly becomes an issue for your users.

+1 to this. Don't solve things that are not problems.

michaeldeol said:

The big win will be making sure you are letting your user know what is happening by alerting them, flash message etc...

But instead of restricting another buyer from taking a product into his cart, you may just let him know that it's also in another person's cart. And who checks out first is the winner. :D

Last updated 1 year ago.
0

vernard said:

But instead of restricting another buyer from taking a product into his cart, you may just let him know that it's also in another person's cart. And who checks out first is the winner. :D

Thanks for all your answers. I have already done some user tests, and it's a real issue for them to add a non available product to their carts. But your idea to let them know that the product is also in another user's cart is very good. How can i do that? What do you think about a cart variable in the cache (APC)?

Last updated 1 year ago.
0

soclosed said:

What do you think about a cart variable in the cache (APC)?

I have not used ( or maybe subconciously used ) APC but I've done some research. Correct if me I'm wrong but according to what I read, APC is not a suitable solution for this problem as the cart changes frequently and can be different for every user.

The database solution that you suggested might work. Here's a case...

You can add a datetime field in the cart table and update that field every time the user interacts with the server. Set a time interval of the maximum time the product can stay in the cart. And when the difference of the cart item's datetime and the current datetime reaches that time interval, you remove it from the user's cart. Also, remove all the items from the cart upon user logout.

And when the user views the cart or when a user adds an item in cart, you can perform a check if the product exists in other user's cart then notify them both.

Sample scenario would be when User1 already has Item1 in his cart, and then User2 adds the Item1 in his cart.

User1 should be notified that another user added that item in his cart, and that he should hurry before User2 checks out. User2 should also know that Item1 is already in another user's cart and he can still make it if he checks out first..

It would also be nice to notify them if the other user checks out with the Item1, and that item will be removed from his cart.

If the product entity has a quantity attribute, there would be extra complications as this kind of notification should not happen if the item has at least 2 quantities. Hence, it should not display this notification if there are 3 user's having that item in the cart and the item has at least 3 quantities.

The removal of item from the cart should only happen if the product's available quantity is 0. Since he still has a chance to check out with it if there's at least 1 quantity.

vernard said:

I believe that this would complicate your application with very minimal profit.

But if you think it's worth the work, then you may try this solution.

If anyone thinks of a better way to do it, please inform us.

Last updated 1 year ago.
0

Thank you vernard for this complete answer !

It's a great solution, but there is a problem for one case : If the user1 adds items to his cart and doesn't interact with the server during the specified time interval, until he leaves the website. It means that the cart still exist in the database, so everytime user2 will add the item to his cart, it will be notify, even if user1 is not present on the website (because he doesn't want his cart anymore). A solution can be to check the datetime each time a user add an item to his cart. What do you think about it?

For APC, i have no experience about this system. But i read some good thing about it that match with a cart :

  • When you put a variable in the cache, you can set a time-to-live parameter, so no need to handle your own system of datetime interval

  • Every sessions can access to this variable, so all connected users can know the products added to carts, so no need to communicate with the database

Last updated 1 year ago.
0

A simple solution is the following:

  • Have a shopping cart database table that tracks which items are in which user's cart
  • Have a timestamp field that marks when the item was added to the cart
  • You can optionally have a timestamp field that updates each time the user interacts with the cart, as mentioned before
  • Run a cronjob every 'x' number of minutes that grabs all the items from the cart database table, checks how long the items have been there (or how long since last updated), and if it's beyond your set threshold (i.e. 2 hours) remove it from the database

You could implement this functionality is an artisan command line task (which is very easy to do http://laravel.com/docs/commands#building-a-command) and then have your cron job run that task every minute, 5 minutes, 10 minutes etc.

Edit:

Regarding your previous post, it seems reasonable to do it with caching as well, although I think you could do it even with the file based driver. All you need is a cache key for each item in the cart (i.e. $key = "cart.{$item->id}") and set an expiry for it. Then when someone is browsing products see if that item exists in the cache.

Last updated 1 year ago.
0

Thanks for your response crhayes, it is very usefull !

I will try both methods, its seem goods. For performance, do you think it's more efficient with the cache or with the database?

Last updated 1 year ago.
0

Unless you're doing millions of transactions I doubt there is much of a difference.

I'd say get it working with whatever method you feel most comfortable with, and if efficiency becomes a problem in the future you can fix it then. With that being said leave your code as flexible as you can so that you are not stuck with one method or the other for eternity :)

Last updated 1 year ago.
0

Great advice, thanks ! :)

Last updated 1 year ago.
0

No problem, glad I could help :)

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

soclosed soclosed Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.