Jump to content

Empty cart contents when user logs out


djlfreak

Recommended Posts

How do I word this function so the cart contents are emptied when user clicks log out button? I'm thinking it would be something like WHERE user_id= $_SESSION[''];

 

 

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  :D

 

Last post I promise, just trying to finish off website and had a few issues can't fix.

Link to comment
https://forums.phpfreaks.com/topic/203848-empty-cart-contents-when-user-logs-out/
Share on other sites

Where do you store your cart contents in session? db? or both? You must realize that very little people actually press the logout button. So if you base your logic on this event then in most cases you won't be clearing at all.

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 as you 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 the cart from old user was still there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.