Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active April 6, 2023 17:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save evgv/48b5fbd02b0cfe74eacc9c280e40b644 to your computer and use it in GitHub Desktop.
Save evgv/48b5fbd02b0cfe74eacc9c280e40b644 to your computer and use it in GitHub Desktop.
Magento. The Magento Checkout Object and Session Object.

The Magento Checkout Object and Session Object

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

  $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

// 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

// 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()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment