I am doing a food shopping website. I give a random number to any visitor on the site generated by rand() and when they add to cart, i give the cartNo that particular visitor No. However, if a visitor has added to cart and not proceeded to checkout, the items will remain in the cart and the next time they open the browser, they wont ever get that particular cartNo to which their items were added. I want to empty the cart in case that scenario happens. How do I do that?
Asked
Active
Viewed 41 times
0
-
1Store the date/time when the cart was last updated, and run a CRON or admin job periodically to clean up un-finished carts over a certain age? The issue is knowing whether someone else might get that cart and it contents, depending on how you generate a random number. Why not use a sequential number that never loops? – droopsnoot May 01 '20 at 12:08
-
@droopsnoot that idea seems good. admin job part? Do i implement that by a query? The rand num is long – May 01 '20 at 12:11
-
It would be a query, to remove all rows where the last updated date is more than whatever age you choose. I only make the point about random numbers because computers don't do random very well, whereas they do incrementing very well. – droopsnoot May 01 '20 at 12:38
-
Do you store visitorNo or cartNo in the session ? I wonder if you do that so cart won't be empty if they close and reopen the browser. totally it's not a good idea to reset their cart by closing and reopening the browser. you want the cart to be empty in this case intentionally? – Razi May 01 '20 at 14:48
-
@droopsnoot yeah but where do i execute this query? Was thinking of doing it on my homepage but wont it slow the page? – May 01 '20 at 15:27
-
@rziw the cartNo is the visitorNo itself. I want the cart to be empty because if a user has closed the browser and come again, he will just get another visitorNo. His items will remain lying in the cart and wont be accessed again – May 01 '20 at 15:30
-
Well you may set visitorNo in session and as you have this number in database (i suppose you have), whenever user opens the browser again check if there is already a cart with this number (from session) , delete it by a query , then you may also remove the session of visitorNo – Razi May 01 '20 at 16:05
-
@rziw but how will i get this num again? It is randomly generated – May 01 '20 at 17:32
-
when you create this number randomly, store it in a session for each visitor, then when visitor opens browser again, check if session has visitor number : if(isset($_SESSION['visitor_number'])) , then get it from the session. (and I still think even storing cart number in a session in order to find the cart later works but I supposed you don't want it) – Razi May 01 '20 at 18:05
-
@rziw But when i reopen browser, aint that session lost? How will i cross chk that? – May 01 '20 at 20:13
-
No it's possible that session not being lost, check the accepted answer here : https://stackoverflow.com/questions/20345640/session-destroy-on-browser-close – Razi May 01 '20 at 20:40
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212939/discussion-between-rziw-and-ox1234567). – Razi May 01 '20 at 20:41