LaravelShoppingcart
LaravelShoppingcart copied to clipboard
How to reserve & expire quantity when adding items to the cart?
For some reasons, I have a requirement of expiring the cart in 5 minutes. I didnt find it in config/cart.php. Is there any option for this?
Can you elaborate more on the expire part? What exactly do you mean by "expiring the cart" and how will the timeout be renewed?
Out of my head and assuming i am correctly understanding the question i would do something like the following:
- Write an "accessor" for the cart to wrap it into with your identifier as parameter
- Before returning the cart instance, check if it is older than 5 min by setting some variable using the Cache facade
- Cart in DB also has nullableTimestamps so you could use that to once and a while check for Carts that are overdue and delete them with a single query to remove unneccessary data
Maybe you could also do this by using the service container.
Sir actually the scenario is this :
I am using this package for a canteen system to order items. My requirement was upon adding to cart, my stock gets reduced. So, if one person adds to the cart and goes idle, the item would be reserved for until session expires.
That's why I need to expire the cart in 5 minutes and invoke a action that could restock the items. I am planning to move ahead with the database part as you said.
There is one issue with this that comes to my mind: The cart content is stored in the session or in the database (in a single row) so you can not do a query to determine the correct, avaliable qty to display on the product page. If the content of the cart was DB only the available qty would simply be qty - sum(qty) of all CartItem positions in DB
. With the solution above you would need to check all carts to get the total qty currently in use by other carts. This is also a reason why this package is not good to use with API (since the lack of session).
@bumbummen99 sir I thought of a workaround :
So, I am planning to remove cart from the session data located inside storage/framework/sessions___ file. I am trying to track user's session_id in a virtual table along with every cart add/update and then on basis of that session_id with cart_updated_time. I am trying to run a cron job, which upon created every 5 minutes, IF NOT COMPLETED THE ORDER, will restock the quantity of the items and remove cart data from the user's session file based upon that same session_id.
Problem is I stored the session_id of the user and everything is working well, I am having trouble retreiving user's session data based on the session_id, could you suggest any mechanism for this?
$session = session();
dd($session);
I got this random sesison ID EE7868eFef...
This file exists in storage/framework/sessions/____
I want the content of that file using sessionID. Because I need to access that session data using cron job.