1

I got one site to fix issues and noticed one very odd issue.

Below code is work well in DISABLE cache mode but not in ENABLE mode. If I enable cache then it only works on customer-account (customer/account) section but not other parts/pages of site.

Code snippet of path:

app/design/frontend/Company/developer/Magento_Theme/templates/html/header.phtml

$objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManagerlogin->get('Magento\Customer\Model\Session');
$baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
 if($customerSession->isLoggedIn()) {

      echo "Logout";

 }else { 
      echo "Login";
}

PS: I am aware that Object Manager should not be used and commands (static content, reindex, cacha clean, di compilation) ran completely.

  • please let me know if you have issue – Rakesh Jesadiya Jun 8 '17 at 5:45
  • You have to add this code in your block file – Rakesh Jesadiya Jun 8 '17 at 7:02
  • You cannot solve it via PHP while using FPC. Every dynamic part should be handled via KnockoutJS in Magento2. – Phoenix128_RiccardoT Jun 10 '17 at 15:30
  • I sorted it and will post the answer as well. thx – Jai Jun 12 '17 at 3:50
-2

You can use below way to get current customer login status,

public function __construct(
        \Magento\Framework\App\Http\Context $appContext
    ) {
        $this->appContext = $appContext;
    }

    protected function isLoggedIn()
    {
        return $this->appContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);           
    }
  • How to call this in header.phtml file? and one more thing that WHY question code behave different in enabled-cache? – Jai Jun 9 '17 at 5:00
  • $objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManagerlogin->get('Magento\Framework\App\Http\Context')->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); if($customerSession->isLoggedIn()){//your code}else{//your code} try with direct code – Rakesh Jesadiya Jun 9 '17 at 5:03
  • Direct code is not working. Before that, I created a block and called that isLoggedIn but that also created problem. Can you please update question with that included creating block and calling so that i check what i missed? – Jai Jun 9 '17 at 5:41

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.