13
votes

Magento has two ways to store a cart. Logged In users can have cart saving defined for as long as you want to define it and it is stored in the database tied to the user number. Non-logged in users seem to be bound by how long your site keeps it's session variables. This leads me to 2 questions.

1) Am I correct in thinking that non-logged in users carts are tied to session timeouts?

2) Since Magento/Varien recommends fairly short times for killing session variables (usually only 4 hours), if question one is true, is there a way to keep a non-logged in cart around without changing the session time out variable?

3

3 Answers

22
votes

As I understand it carts are saved as quotes, even for guests. Logged in users have a customer ID which is stored with the quote, guests do not so their quote has a null customer ID, hence you may find a store has a lot of orphaned/incomplete quotes in the DB. The only way to associate a guest with their cart is by storing the quote ID in their session.

You could extend how long their quote is available to them by storing the quote ID directly in their cookie with a long timeout but this leads to an obvious security breach; anyone could adjust the value in their cookie and view anyone else's cart.

The only safe way is to proceed is to create a table of guest tokens and associate it with quotes (sorry no code this time, there's too much to explain in a low level). The token is the only public part and is set in the cookie. Tokens should be random and long, say 512-bits/64-chars, but not too long because they are included in every HTTP header. Every time a new session is created it might be a returning guest so check for a token and look it up in the table. Take the found quote ID and store that in the session thereby resurrecting the old cart. Quotes with customer IDs don't need to be rescued this way so should be exempt, especially since a logging-out customer doesn't want to see any part of their account remain visible.

9
votes

Take a look into your magento database at the table "sales_flat_quote"

Regards boti

1
votes

Carts are saved to the 'sales_flat_quote' table

The items in the basket are saved to 'sales_flat_quote_item', linked by the quote's entity_id

Finally the options of the items are saved to 'sales_flat_quote_item_option' linked by the item_id above

Therefore to view all items and options for a saved quote

select sfqi.item_id, sfqio.code, sfqio.value from sales_flat_quote AS sfq, sales_flat_quote_item AS sfqi, sales_flat_quote_item_option AS sfqio where sfqi.item_id = sfqio.item_id AND sfqi.quote_id = sfq.entity_id AND sfq.entity_id = '133940';