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.
    – Wolfack
    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
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.

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.