2

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>';
     }
    }
}
2
  • please let me know if you have issue Aug 3, 2017 at 6:05
  • when full page cache enable it will not work like that way there is another way to check this please find it on google it may help you. Jul 29, 2019 at 13:50

2 Answers 2

1

You have to set cacheable="false" your module with xml file like this,

<block class="Namespace\Module\Block\Link" name="customname" cacheable="false">

Remove var/generation and clear cache.

0

Simple add this block code in cms or block content

{{block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link" template="account/link/authorization.phtml"/}}

it will show you signin or signout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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