3

I am instantiating the customer session helper (\Magento\Customer\Model\Session $customerSession) correctly, but this function always returns false, even though the customer is logged in. What am I missing here?

2
  • 1
    Give us your code Commented Mar 31, 2017 at 9:28
  • 1
    Have you try below answer code ? Commented Mar 31, 2017 at 13:44

4 Answers 4

8

If store cache is enable, you can not get customer session. Check below code for get customer session with enable cache.

/** * @var \Magento\Customer\Model\Session */ protected $_customerSession; public function __construct(Template\Context $context, \Magento\Framework\App\Request\Http $request, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Model\SessionFactory $customerSession ) { $this->request = $request; $this->customerRepository = $customerRepository; $this->_customerSession = $customerSession; parent::__construct($context); } public function getCustomerId(){ $customer = $this->_customerSession->create(); var_dump($customer->getCustomer()->getId()); } 

Write above code in block, It is working even cache is enable.

2
  • Great, saved my day. Commented Jul 16, 2019 at 12:13
  • This is the right answer ! FPC caches the Customer Session object as well. So we need to create a fresh one using SessionFactory while FPC is enabled. Thanks for the help. Commented Jul 17, 2019 at 7:29
1

I solved the problem by adding cacheable=false for that block, in the layout .xml file.

3
  • 2
    Sadly now your page will not be cached by Full Page Cache -- devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/… Commented Mar 12, 2019 at 13:54
  • Not recommended. This will forbid caching of your whole page. Commented Jul 16, 2019 at 12:17
  • @Vladimir this is not recommended at all. Need to understand the architecture of Magento 2 Commented Feb 13, 2024 at 14:27
0

I think this solution will work. Haha my english is a bit poor so my explanation is not good. You can check out the materials here : https://aureatelabs.com/blog/how-to-check-customer-is-logged-in-or-not-when-a-cache-is-enabled-in-magento-2/

namespace SnaptecHue\Customer\Block; use Magento\Customer\Model\Session; use Magento\Framework\View\Element\Template; class CustomerCustomInfo extends Template { protected $httpContext; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Http\Context $httpContext, array $data = [] ) { $this->httpContext = $httpContext; parent::__construct($context, $data); } public function getCustomerIsLoggedIn() { return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); } public function getCustomerId() { return $this->httpContext->getValue('customer_id'); } public function getCustomerName() { return $this->httpContext->getValue('customer_name'); } public function getCustomerEmail() { return $this->httpContext->getValue('customer_email'); } } namespace SnaptecHue\Customer\Plugin; class CustomerSessionContext { /** * @var \Magento\Customer\Model\Session */ protected $customerSession; /** * @var \Magento\Framework\App\Http\Context */ protected $httpContext; /** * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\App\Http\Context $httpContext */ public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $httpContext ) { $this->customerSession = $customerSession; $this->httpContext = $httpContext; } /** * @param \Magento\Framework\App\ActionInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return mixed */ public function aroundDispatch( \Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { $this->httpContext->setValue( 'customer_id', $this->customerSession->getCustomerId(), false ); $this->httpContext->setValue( 'customer_name', $this->customerSession->getCustomer()->getName(), false ); $this->httpContext->setValue( 'customer_email', $this->customerSession->getCustomer()->getEmail(), false ); return $proceed($request); } } 
-1

Try this :

protected $_session; public function __construct( ... \Magento\Customer\Model\Session $session, ... ) { ... $this->_session = $session; ... } public function isLoggedIn() { if ($this->_session->isLoggedIn()) { // Customer is logged in } else { // Customer is not logged in } } 

Note: This works for frontend customers only. So If you are checking in admin section then it won't work.

Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.