I have a block to add link at top based on if customer login like this, when i'm at myaccount page the header link show the right one, but when i go to other page it shows the wrong one, here's my code:
<?php
namespace Namespace\Module\Block;
class Link extends \Magento\Framework\View\Element\Html\Link
{
protected function _toHtml()
{
if (false != $this->getTemplate()) {
return parent::_toHtml();
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
return '<li><a href="/test/loggedin" > Logged In </a></li>' ;
} else {
return '<li><a href="/test/notloggedin" > Not Logged In </a></li>';
}
}
}