Results 1 to 5 of 5

Thread: Delete Abandoned cart when user loggs out

  1. #1
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delete Abandoned cart when user loggs out

    How do I word this function so the cart contents are emptied when user clicks log out button?

    PHP Code:
    function deleteAbandonedCart()
    {

        
    $sql "DELETE FROM tbl_cart
                WHERE ??????   '"
    ;
        
    dbQuery($sql);        
    }

    ?> 
    PHP Code:
    //LOGOUT FUNCTION
            
    case 'Logout':
            
    session_start();
            
    session_unset();
            
    session_destroy();
            
    redirect('index.php');
            break; 
    Thanks in advance for any help

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cart info stored in in session cookie.

    Thanks so much for your reply.
    Sorry I didn't explain properly, my fault. I store cart contents in session id like this

    PHP Code:
    // 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?

    PHP Code:
    /*    Delete all cart entries older than one day*/
    function deleteAbandonedCart(){    
    $yesterday date('Y-m-d H:i:s'mktime(0,0,0date('m'), 
    date('d') - 1date('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.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by slimline77 View Post
    ... 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.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •