How to manage session data in Magento 2?

Managing session data is a complex process consisting of different problems, which requires knowledge of code and programming skills. 

Warning: all code snippets in this article are examples. You need to customize them to suit your business needs.

Issue #1. Getting a Magento checkout or customer session

You can set and get a ?ustomer session in Magento 2 by using Magento\Customer\Model\Session:

protected $customerSession; public function __construct( \Magento\Customer\Model\Session $customerSession ){ $this->customerSession = $customerSession; } 
$this->customerSession->setMyValue('test'); $this->customerSession->getMyValue();

If you want to set information for a ?ustomer session, then do the following:

$this->customerSession->setMyValue('test');

To get information from the customer session, do the following:

$customerSession->getMyValue();

The session will expand the Magento\Framework\Session\SessionManager core class to handle the session.

See how to configure customer session lifetime in Magento 2 → 

Issue #2. The Current Session Has Been Expired error

One more issue you may face with the admin session is the Current Session Has Been Expired error. If it appears, then it may be because no path has the admin/security/session_lifetime value on the core_config_data table of your database.

There is an option to fix this in the admin panel, not with the use of code: Stores -> Configuration -> Advanced -> Admin -> Security -> Admin Session Lifetime (seconds).

For all other sessions - you need to configure the session.cookie_lifetime directive in php.ini PHP settings.

You can add the 1 row of the following code:

config_id : NULL (automatically)
scope     : default
scope_id  : 0
path      : admin/security/session_lifetime
value     : 86400 (1 day by 60*60*24, it's in second

Insert it to the core_config_data table

After that, clean the cache : php bin/magento cache:clean or bin/magento cache:clean > hard refresh the browser > sign in.

Issue #3. Customer name is not displayed 

You may face the problem that the customer name is not displayed when logged in to Magento 2. Pages are cached, which means you need to use JavaScript and customer_data from sections, not backend code. 

Issue #4. Getting registered customer session info in Magento 2

Your leads are potential sales. Collecting data about registered customers can help you find information that will allow you to understand who your customers are, what their desires are, and what approach to take in working with them. Magento 2 store admin will be able to implement various features based on this data. You can only allow registered customers to see pricing information. It is also possible to offer delivery services only to certain groups of customers.

The administrator needs to get the client attributes for such features. So how do you get registered customer session info in Magento?

Using the Block class:

protected $customer;
public function __construct(\Magento\Customer\Model\Session $customer,)
{
    $this->customer = $customer;
}
public function yourMethodName()
{
    $customer = $this->customer;
    $customerName = $customer->getName();
    $customerId = $customer->getId();
//You will get all basic detail with this $customer object
}

And to segment your customers more efficiently, you can use the Customer Segmentation extension. You can split them into groups by orders, shipping and billing addresses, shopping cart information, and many other parameters using this extension. Moreover, the Customer Segmentation extension allows store administrators to segment not only registered customers but also guest users, getting their session info. 

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.