3

This question already has an answer here:

I'm trying to create a piece of code to check if the customer is loggen in or not. I read not to use the objectmanager for that, but I don't know where to put which code.

What I need:

if customer is logged in { Go to my account } else { Login }

How can this be achieved?

I'm using Magento 2.2.6

marked as duplicate by Piyush, Dinesh Yadav, Manashvi Birla, sv3n, Rohan Hapani Oct 9 '18 at 11:12

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.

  • where you want to check this? in controller ? – Rutvee Sojitra Oct 9 '18 at 10:00
  • I need to do this in: app/design/frontend/theme/themename/Magento_Theme/templates/html/header.phtml – n00bly Oct 9 '18 at 10:02
  • Please check my answer and let me know if not solve your question. – Rohan Hapani Oct 9 '18 at 10:19
0

You can use the below code as below:

protected $_custSession;

public function __construct(
    .....
    \Magento\Customer\Model\Session $session,
    .....
) {
    .....
    $this->_custSession = $session;
    .....
}

//later in your code you can use it as below

if ($this->_custSession->isLoggedIn()) {
    // if customer is logged in 
} else {
    // if customer is not logged in
}

Since you are asking your question in phtml you can use the below code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customSession = $objectManager->get('Magento\Customer\Model\Session');
if($customSession->isLoggedIn()) {
   // customer is logged in
}
else{
  //customer is not logged in
}
  • 1
    Great! It's working :) I don't know if this is the best and only method to use but it's working at my side. – n00bly Oct 9 '18 at 11:10
0

You can make custom helper in and can call in app/design/frontend/theme/themename/Magento_Theme/templates/html/header.phtml

like below:

$helper = $this->helper('Vendor\Module\Helper\Header'); and you can call like:

$helper->isCustomerLoggedIn();

In Vendor\Module\Helper\Header Class you can make one method for check customer is logged in or not

                 <?php
                  namespace Vendor\Module\Helper;

            class Header extends 
           \Magento\Framework\App\Helper\AbstractHelper
             {

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Customer\Model\Session $customerSession
        ) {

    $this->_customerSession = $customerSession;
    parent::__construct($context);
}

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

}

  • Hi Rutvee, I which file should I put which piece of code? – n00bly Oct 9 '18 at 11:07
  • make one file Vendor\Module\Helper\Header with above code which answered by me – Rutvee Sojitra Oct 9 '18 at 11:08
  • and check in app/design/frontend/theme/themename/Magento_Theme/templates/html/header.phtml using $helper = $this->helper('Vendor\Module\Helper\Header'); $helper->isCustomerLoggedIn(); – Rutvee Sojitra Oct 9 '18 at 11:09
0

Try to use this below code :

protected $_customerSession;
protected $_url;
protected $_response;


public function __construct(
    .....
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\UrlInterface $url,
    \Magento\Framework\App\ResponseInterface $response
    .....
) {
    .....
    $this->_customerSession = $customerSession;
    $this->_url = $url;
    $this->_response = $response; 
    .....
}

if ($this->_custSession->isLoggedIn()) {
    //redirect to my account page
    $this->_response->setRedirect($this->url->getUrl('customer/account'))->sendResponse();
} else {
    //redirect to login page
    $customerSession->setAfterAuthUrl($this->_url->getCurrentUrl());
    $customerSession->authenticate();
}

Remove generated and clean cache.

  • Hi Rohan, where should I put this code, in which file? – n00bly Oct 9 '18 at 11:05

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