4

This is my code below

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();    
$customerSession = $objectManager-create('Magento\Customer\Model\Session');

   if ($customerSession->isLoggedIn()) 

   {

       $customerSession->getCustomerId();  // get Customer Id
        $customerSession->getCustomerGroupId();
        $customerSession->getCustomer();
        $customerSession->getCustomerData();

   }

I'm facing a problem when full page cache is enable on disable it's working fine.

5
  • Where are you using this code and what are you trying to achieve?
    – hungersoft
    Commented Dec 3, 2018 at 5:04
  • I am using this code in phtml file to open a popup form after login
    – Unknown
    Commented Dec 3, 2018 at 5:08
  • The following might help: magento.stackexchange.com/questions/145851/…
    – hungersoft
    Commented Dec 3, 2018 at 5:12
  • I have faced this problem before. Solution is please do not use objectManager. Create function in helper or block and use that. Commented Dec 3, 2018 at 5:18
  • Check my answer and let me know if not working. Commented Dec 3, 2018 at 5:27

3 Answers 3

14

You need to use the factory method when FPC is enabled.

By Factory Method :

/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSessionFactory;

public function __construct(
    ....
    \Magento\Customer\Model\SessionFactory $customerSessionFactory
    ....
) 
{
    ....
    $this->_customerSessionFactory = $customerSessionFactory;
    ....
}

public function getCustomerId(){
  $customer = $this->_customerSessionFactory->create();
  echo $customer->getCustomer()->getId();
}

By Object Manager :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $objectManager->get('\Magento\Customer\Model\Session');

echo $customerSession->getCustomerId();
3
  • I have tried above code but still not working
    – Unknown
    Commented Dec 3, 2018 at 5:44
  • Did you remove generated folder? and flush cache Commented Dec 3, 2018 at 5:48
  • Yes i did still same issue
    – Unknown
    Commented Dec 3, 2018 at 5:53
6

You can give a try to below code:

1). Using Object Manager

$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
return $isLoggedIn;

2). DI way

/**
* @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());
}

It will work even FPC is enabled.

1
  • 1
    Works like charm ;)
    – Volvox
    Commented Mar 26, 2019 at 8:59
1

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

\\Use create instead get in object manager

$customerSession = $objectManager->create('\Magento\Customer\Model\Session');

echo $customerSession->getCustomerId();

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.