The Checkout Object and Session Object

** TODO: Mage_Checkout_Model_Cart **

The Magento Session object should be used when querying the current quote. Access it like so:

 <?php $checkout = Mage::getSingleton('checkout/session')

The getQuote() method is responsible for retrieving the current quote. If a quote ID doesn’t exist on the session object then a new quote object (Mage_Sales_Model_Quote) is created and set up with all of the necessary data (customer id, store id, remote ip etc).

If a product has never been added to the cart or the cart isn’t being loaded from a previous session, it’s likely the cart will never have been saved and will have no id. The cart gets saved when a product is added from the Checkout module’s CartController (Mage_Checkout_CartController) in the addAction.

Once the cart has been saved, the checkout session only retains the database Id of the cart, and uses that to reload the cart on subsequent getCart() calls. The quote id persists the session because it’s set on the data object of the checkout session object. Other properties (such as _quote) do not and are reloaded when requested.

Useful Methods – checkout/session

<?php
// Nullifys the quote
Mage::getSingleton('checkout/session')->unsetAll()

// Checks whether a quote exists
Mage::getSingleton('checkout/session')->hasQuote()

// Gets the currently active quote, or creates one if a quote id is not set on the session object
Mage::getSingleton('checkout/session')->getQuote()

// Get the Quote ID
Mage::getSingleton('checkout/session')->getQuoteId()

// Clears the quote and dispatches event checkout_quote_destroy
Mage::getSingleton('checkout/session')->clear()

// Get the last real order ID
Mage::getSingleton('checkout/session')->getLastRealOrder()

Events Dispatched

<?php
// At the start of the getQuote() method
Mage::dispatchEvent('custom_quote_process', array('checkout_session' => $this));

// In the getQuote() method, if no customer is logged in and there's no customer object on the session
Mage::dispatchEvent('checkout_quote_init', array('quote'=>$quote));

// In the loadCustomerQuote() method, when an attempt is made to retrieve a customer's quote
Mage::dispatchEvent('load_customer_quote_before', array('checkout_session' => $this));

// In the clear() method:
Mage::dispatchEvent('checkout_quote_destroy', array('quote'=>$this->getQuote()));