17

I want to use magento session to trace a customer session, but couldn't find a link between above 3 session types.

What are the main differences of these 3 types?

Why does Magento have 3 session types instead of one?

And how are they linked together?

BatMUD
  • 307
  • 1
  • 2
  • 10

2 Answers2

36

Core/Session

This is the most bare bones session. It give the basic "anonymous" data about the visitor (cookies, IP address, error messages).

Mage_Core_Model_Session::getCookie()
Mage_Core_Model_Session::addMessage()
Mage_Core_Model_Session::useValidateRemoteAddr()

Customer/Session

This object handles things related to the specific customer (logging in or out, name, id, email, the customer's group)

Mage_Customer_Model_Session::getCustomerId()
Mage_Customer_Model_Session::isLoggedIn()
Mage_Customer_Model_Session::getCustomerGroupId()

Checkout/Session

This stores information related to the quote, guest or not (cart totals, items, checkout progress)

Mage_Checkout_Model_Session::getQuote()
Mage_Checkout_Model_Session::setStepData()
Mage_Checkout_Model_Session::getQuote()->getTotals()

Each of the session models are extended from Mage_Core_Model_Session_Abstract, they just offer different functionality. There really isn't much to core/session. Most of it's functionality is inherited from the parent class that they all share.

You can relate the customer/session to the checkout/session with their various methods.

e.g.

Mage::getSingleton('checkout/session')
    ->setCustomer(Mage::getSingleton('customer/session')->getCustomer());

etc

P.S. Don't forget about adminhtml/session!

Shadoweb
  • 5,812
  • 1
  • 42
  • 55
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • So accessing the billing form email address in the checkout would not work like this ` $customer = Mage::getSingleton('customer/session')->getCustomer(); $customerEmail = $customer->getEmail();` because one would have to use checkout session? – snh_nl Mar 08 '18 at 15:04
8

Magento introduces grouping to manage session data for different usage that differentiates it from its counterparts. Let’s dig into the details. All session data in Magento is stored in global variable $_SESSION, an array from programming view, and categorizes them into independent groups with each group represented by an array encapsulated by a session class.

Magento by default is equipped with three session classes for core, customer and checkout.

Mage::getSingelton('core/session'), Mage::getSingleton('customer/session'), Mage::getSingleton('checkout/session') are three session functions used in Magento. $_SESSION variable finally has the form of the following:

$_SESSION=array('core'=>array(...), 'customer'=>array(...), 'checkout'=>array(...),...);

we should avoid to directly operate on $_SESSION variable, and instead act in Magento’s way, like Mage::getSingleton('core/session')-getXXX() and Mage::getSingleton('core/session')->setXXX().

If a customized session class is needed, it is required to inherit Mage_Core_Model_Session_Abstract. Below is an example:

class Company_Module_Model_Session extends Mage_Core_Model_Session_Abstract
{
    public function __construct() {
        $this->init('module');
    }
}

And then we can use this customized session class by calling Mage::getSingleton('mgwishlist/session'), just as any other session classes in Magento.

Particularly, PHP session can only keep values of basic data types, such as int, bool, string and etc, so serialization/deserialization is frequently touched.

Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
Afroz Alam
  • 874
  • 6
  • 19