0

I have tried to obtain the logged in customer details by using all the below models. Nothing seems to work. Are there any other ways to obtain the user data?

    \Magento\Backend\Model\Auth\Session $backendAuthSession,
    \Magento\Checkout\Model\Session $currentSession ,
    \Magento\Customer\Model\SessionFactory $customerSession,
4
  • please check my answer and let me know if not working. Commented Nov 30, 2018 at 6:02
  • Will do. I was Injecting the above three models the way you have said. But not the \Magento\Customer\Model\Session model itself. Commented Nov 30, 2018 at 6:08
  • Please upload your full code here. Commented Nov 30, 2018 at 6:08
  • It did not work. Do I need to make any changes to app/etc/di.xml ?. I am trying to render this in a PHTML Commented Dec 3, 2018 at 14:18

1 Answer 1

1

You need to inject \Magento\Customer\Model\Session in your construct

protected $_session;

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

Then add this below code in your function

if ($this->_session->isLoggedIn()) {
    echo $this->_session->getCustomer()->getId(); //Logged in customer id will display
    echo $this->_session->getCustomer()->getName(); //Logged in customer name will display
} else {
    echo "customer not logged in";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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