0

I am trying to get loggedIn customer ID, I logged in and it is also showing in Online customer grid in the backend also can see on frontend. But if I check by

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   echo "login";
}

It gives out as not logged in. (using DI way also result is same)

Also

$ID = var_dump($this->_customerSession->isLoggedIn());
    echo $ID;

It gives output as:

bool(false)

How can I get Logged In customer ID from session ?

1

You can't use session in fullpage cache. Magento already removed session Instead use Magento\Framework\App\Http\Context inject in your block class for check customer logged in or not. You can disable block cache but this cause entire page not cacheable. This is not recommended

0

For debugging what about this

/** @var \Magento\Framework\App\ObjectManager $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\Http\Context $context */
$context = $om->get('Magento\Framework\App\Http\Context');
/** @var bool $isLoggedIn */
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

Also you've said about getting I'd

Something like this should work

public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }

    public function whatever()
    {
        var_dump($this->customerSession->getCustomer()->getId());
        exit();
    }
  • when fullpage cache enable generic session class will not work as you expected – Zaheerabbas Jun 27 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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