In LoginPost controller using this "Magento\Customer\Model\Session" session. After Login I need to add a custom array to the current customer session. how can i add a custom data to magento customer session? please help me with your valuable answer.


You need to create customer_login event observer for set value in customer session after login

1) Create events.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />

2) After this file, you need to create your observer file



namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
    protected $customerSession;

    public function _construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;

    public function execute(\Magento\Framework\Event\Observer $observer)
        $customer = $observer->getEvent()->getCustomer(); //Get customer object
        $myArray = array('value1','value2');
        $setValye = $this->customerSession->setMyValue($myArray); //set value in customer session
        $getValue = $this->customerSession->getMyValue(); //Get value from customer session

Now run this commands:

php bin/magento cache:flush

php bin/magento setup:di:compile

  • Thank you somuch For Your valuable feedback. Its helped me a lot – Midlaj Jan 12 '18 at 5:54
  • Yr welcome.. :) – Prince Patel Jan 12 '18 at 6:02
  • i can't use print_r for $customer, How can i print the $customer? – Midlaj Jan 12 '18 at 8:35
  • You can print $customer object by print_r($customer->getData()) or print_r($customer->debug()) – Prince Patel Jan 12 '18 at 8:40
  • Yea its working.. – Midlaj Jan 12 '18 at 8:46

You can do this by setting any variable using \Magento\Customer\Model\Session as follows:

protected $customerSession;

public function __construct(

    \Magento\Customer\Model\Session $customerSession, 

    $this->customerSession = $customerSession;



array (size=2)
  0 => string 'anime' (length=4)
  1 => string 'midlaj' (length=3)

Actually you can set and get this value across any file using same method.


You can Set and get Customer session by using \Magento\Customer\Model\Session

protected $customerSession;

public function __construct(

    \Magento\Customer\Model\Session $customerSession, 

    $this->customerSession = $customerSession;


You can also set by objectManager , But it's wrong way :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setMyValue('testing value');

For setting an information to the customer session :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$session = $objectManager->get('Magento\Customer\Model\Session');
$session->setTestKey('testing value');

Getting an information from the customer session:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$session = $objectManager->get('Magento\Customer\Model\Session');
echo $session->getTestKey();

Hope this answer helpful for you :)


You can set custom data to customer session like below code.

$customArray = ['value1','value1'];
print_r($this->session->getData()); //Customer session data with custom array data
print_r($this->session->getCustomData()); //Get only custom array data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.