0

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 am facing problem when full page cache is enable on disable it's working fine

  • Where are you using this code and what are you trying to achieve? – hungersoft Dec 3 '18 at 5:04
  • I am using this code in phtml file to open a popup form after login – Parthavi Dec 3 '18 at 5:08
  • The following might help: magento.stackexchange.com/questions/145851/… – hungersoft Dec 3 '18 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. – Mukesh Prajapati Dec 3 '18 at 5:18
  • Check my answer and let me know if not working. – Rohan Hapani Dec 3 '18 at 5:27
5

You need to use 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();
  • I have tried above code but still not working – Parthavi Dec 3 '18 at 5:44
  • Did you remove generated folder? and flush cache – Rohan Hapani Dec 3 '18 at 5:48
  • Yes i did still same issue – Parthavi Dec 3 '18 at 5:53
2

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.

  • If my answer solved your problem, Mark it to accept and give up vote to help community. – Vivek Dec 3 '18 at 5:26
  • 1
    Works like charm ;) – Volvox Mar 26 at 8:59

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.