Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between core/session, customer/session and checkout/session in Magento? [duplicate]

Tags:

magento

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?

like image 340
BatMUD Avatar asked Jul 09 '13 04:07

BatMUD


People also ask

How many types of sessions are there in Magento 2?

Magento2 provides five types of sessions: Magento\Backend\Model\Session– This session is used for Magento backend. Magento\Catalog\Model\Session– Catalog session is used for the frontend for product filters. Magento\Checkout\Model\Session– Checkout session is used to store checkout related information.

How does Magento session work?

To manage sessions, Magento uses the Magento \ Framework \ Session \ SessionManager class. Magento also stores the current session identifier in cookies, which helps the system to identify you as a logged in user, even after you close and open the browser.

How do I create a checkout session in Magento 2?

// get checkout session data echo $block->getCheckoutSession()->getQuoteId(); print_r($block->getCheckoutSession()->getQuote()->getData()); This is the guide to set and unset session in Magento 2. If that you have any queries about the article or any questions in general, use the comment section below!

How do I know if a Magento 2 customer is logged in?

{ ... $this->_session = $session; ... } Then you need to use Magento\Customer\Model\Session::isLoggedIn() to check if the customer is logged in or not.


2 Answers

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!

like image 87
Steve Robbins Avatar answered Oct 19 '22 20:10

Steve Robbins


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.

like image 29
Afroz Alam Avatar answered Oct 19 '22 21:10

Afroz Alam