View Full Version : Delete Abandoned cart when user loggs out
slimline77
06-04-2010, 10:56 AM
How do I word this function so the cart contents are emptied when user clicks log out button?
function deleteAbandonedCart()
{
$sql = "DELETE FROM tbl_cart
WHERE ?????? '";
dbQuery($sql);
}
?>
//LOGOUT FUNCTION
case 'Logout':
session_start();
session_unset();
session_destroy();
redirect('index.php');
break;
Thanks in advance for any help:)
djr33
06-04-2010, 11:54 AM
Since users rarely 'log out' and often just close the browser, etc., it's not possible to do this.
However, since they will get a new cart the next time they log in (right?) and because it won't take a huge amount of space in the database, this isn't a major problem.
You could look into cron jobs to set this to run often, or you could just add a pruning query to the top of every page-- some pseudo-code: DELETE .... WHERE time>15min
That should give you an idea at least.
Unfortunately there's no way to force this to be connected to the user leaving.
You could look into using sessions for this sort of thing.
slimline77
06-04-2010, 03:05 PM
Thanks so much for your reply.
Sorry I didn't explain properly, my fault. I store cart contents in session id like this
// current session id
$sid = session_id();
// check if the product is already
// in cart table for this session
$sql = "SELECT pd_id
FROM tbl_cart
WHERE pd_id = $productId AND ct_session_id = '$sid'";
$result = dbQuery($sql);
At the moment it deletes cart entries older than one day but I want them to delete immediately once user leaves site, not logs out because users can shop without logging in. How would I achieve this?
/* Delete all cart entries older than one day*/
function deleteAbandonedCart(){
$yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'),
date('d') - 1, date('Y')));
$sql = "DELETE FROM tbl_cart
WHERE ct_date < '$yesterday'";
dbQuery($sql);
}
The reason I'm asking is when I was testing it, logging in as different users, I was shocked to see the cart from old user was still there.
... but I want them to delete immediately once user leaves site, not logs out because users can shop without logging in. How would I achieve this?
You can't. Your server (website) has NO IDEA if the user is still looking at the page.
Here's the thing: Users don't "go to" your site - they ask, and you send your site to them. They can then look at it for hours, or the could close their browser before the page is finished rendering. Your server/scripts have absolutely no way of knowing.
Your best bet, as djr said, is to run a cleanup job every fifteen minutes or so, and delete SESSIONs that have had no recent activity.
djr33
06-04-2010, 10:57 PM
Also, there is no reason I can think of that you would need to delete shopping carts. If the session is the same, then they keep the shopping cart. If the session is new, they get a new shopping cart.
This will result in lots of extra shopping carts in your database, but as long as you prune them every day or so (delete any carts not accessed within 24 hours), then you will be fine. Unless you have a HUGE site you won't have problems with filling your database.
Additionally, if you do want to delete the cart when they CLICK log out, then just use something like DELETE .... WHERE sessionid=$sessionid LIMIT 1, and query that in your logout() function. Note that logout will not necessarily change the session id, but it might-- that's related to how your system is setup.
So that will help some, but it won't cover many cases where users just exit your site by closing the window, etc. And it also means that if someone accidentally logs out then can't log back in and see the cart-- this might help some users.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.