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?
- 1Give us your codesyahidah humairoh– syahidah humairoh2017-03-31 09:28:08 +00:00Commented Mar 31, 2017 at 9:28
- 1Have you try below answer code ?Suresh Chikani– Suresh Chikani2017-03-31 13:44:31 +00:00Commented Mar 31, 2017 at 13:44
4 Answers
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.
-
- 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.Ahsan Horani– Ahsan Horani2019-07-17 07:29:30 +00:00Commented Jul 17, 2019 at 7:29
I solved the problem by adding cacheable=false for that block, in the layout .xml file.
- 2Sadly now your page will not be cached by Full Page Cache -- devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/…Volvox– Volvox2019-03-12 13:54:31 +00:00Commented Mar 12, 2019 at 13:54
- Not recommended. This will forbid caching of your whole page.Wolfack– Wolfack2019-07-16 12:17:19 +00:00Commented Jul 16, 2019 at 12:17
- @Vladimir this is not recommended at all. Need to understand the architecture of Magento 2Niraj Patel– Niraj Patel2024-02-13 14:27:05 +00:00Commented Feb 13, 2024 at 14:27
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); } } 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.