When user leaves checkout funnel I need to logout him while preserving his cart contents (so the just logged out user as a guest would see all cart items that he had in a cart when he was logged in). How can I achieve that?
2 Answers
The Mage_Checkout
module declares an observer on customer_logout
event which executes the Mage_Checkout_Model_Observer::unsetAll()
method which in turn calls Mage_Checkout_Model_Session::unsetAll()
whose body follows:
public function unsetAll()
{
parent::unsetAll();
$this->_quote = null;
}
As you see, it is responsible for clearing all data from session, including the quote.
You could be tempted to disable the event observer but I discourage it because it is necessary to clear private data from session.
A better approach could be that of registering your own observer on the same event and call the preserveQuote()
method which copies the quote of the logging-out customer to the already emptied (by the core observer) session as show below:
public function preserveQuote($observer)
{
$customerQuote = Mage::getModel('sales/quote')->loadByCustomer($observer->getCustomer());
$currentQuote = Mage::getSingleton('checkout/session')->getQuote();
$currentQuote->merge($customerQuote);
}
Do your tests because I didn't examine if there can be drawbacks to this solution.
Hope it helps.
Under system > configuration > customers > persistent shopping cart you can enable the persistent shopping cart feature, and make your settings accordingly for how long to keep items in the customer's cart.