5

This question already has an answer here:

How can i check if the customer has logged in to the magento and get the customer details like id, first name, last name, etc.,

I have tried this in 2 ways

1). Using the session in a block as below

protected $_session;
protected $_context;

public function __construct(
    \Magento\Customer\Model\Session $session,
    \Magento\Framework\View\Element\Template\Context $context
) {     
    parent::__construct($context);
    $this->_session = $session;
} 


public function isCustomerLoggedIn()
{
    return $this->_session->isLoggedIn();
}

public function getCustomerId(){
    return $this->_session->getCustomer()->getId();
}

call to the function isCustomerLoggedIn() gives boolean false and call to getCustomerId() returns NULL.

2). Using Object Manager in layout

$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    $customerSession->getCustomer()->getId();
    $customerSession->getCustomer()->getName(); 
    $customerSession->getCustomer()->getEmail();
    $customerSession->getCustomer()->getGroupId(); // get Customer Group Id
}

This way gave the customer Id, Name, Email, Group Id of the previous logged in customer but not the current customer.

Can any one help to get the currently logged in customer details.

marked as duplicate by Raphael at Digital Pianism, Rakesh Jesadiya, Amit Bera, brentwpeterson, Qaisar Satti Sep 26 '16 at 11:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I have checked the marked link and did not work for me. As mentioned in the post first method yielded to Not logged in and second gave the session details of the previous logged customer. – Ranjith Sep 21 '16 at 10:25
3

Try with changing the session object

protected $_session;
protected $_context;

public function __construct(
    \Magento\Customer\Model\Session $session,
    \Magento\Framework\View\Element\Template\Context $context
) {     
    parent::__construct($context);
    $this->objectManager = $context->getObjectManager();
} 


public function isCustomerLoggedIn()
{
    $session = $this->objectManager->get('Magento\Customer\Model\Session');    
    return $session->isLoggedIn();
}

public function getCustomerId(){
    $session = $this->objectManager->get('Magento\Customer\Model\Session');    
    return $session->getCustomer()->getId();
}

In this method replace create from get

$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    $customerSession->getCustomer()->getId();
    $customerSession->getCustomer()->getName(); 
    $customerSession->getCustomer()->getEmail();
    $customerSession->getCustomer()->getGroupId(); // get Customer Group Id
}
  • thanks for your answer, but 'get' returned false on call of $customerSession->isLoggedIn() – Ranjith Sep 21 '16 at 10:28
  • The same code is working for me. It may be a cache issue. Please remove all cache from admin and var/cache. Also remove all cookie of your browser and try it in fresh browser. – Ashish Jagnani Sep 21 '16 at 10:31
  • Checked removing the cache, cookies, browser history but still the same no result – Ranjith Sep 21 '16 at 11:22
  • not working for me... – Sarfaraj Sipai Jun 14 '18 at 10:05
  • not working in magento 2.2.6 – Manish Maheshwari Jun 24 at 7:45
3

Finally I was able to get the customer details using the session in the block. And placing the cacheable attribute for the block specified in the layout as below and it started working.

<block class="Vendor\Module\Block\Sample" name="sample" template="sample.phtml" cacheable="false"/>
  • Do not use cacheable="false". This will disable FPC for that page – Wildcard27 Nov 11 '17 at 6:30

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