0

In Magento 2.2.5, I have used the custom phtml file in app/design/frontend/vendor/theme/Magento_Theme/templates/html/topmenu.phtml to add the customer header links with following code.
But it is showing the login link even loggedIn.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if(!$customerSession->isLoggedIn()) {
    ?>
        <li class="level0 nav-8 last level-top ui-menu-item" role="presentation"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account/login');?>" class="level-top ui-corner-all" id="ui-id-9" tabindex="-1" role="menuitem"><span><?php echo __("Login")?></span></a>
        </li>
    <?php
        }else{
    ?>
    <li class="level0 nav-8 last level-top ui-menu-item" role="presentation"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account');?>" class="level-top ui-corner-all" id="ui-id-9" tabindex="-1" role="menuitem"><span><?php echo __("My Account")?></span></a>
    <ul class="logout-wrap"><li class="" role="menuitem"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account/logout');?>" class="level-top ui-corner-all" id="ui-id-10" tabindex="-1" role="menuitem"><span><?php echo __("Logout")?></span></a></span></li></ul>
    </li>
<?php
    }
?>
  • which file you are using now this code? – Rakesh Donga Jan 30 at 10:33
  • @RakeshDonga, In app/design/frontend/vendor/theme/Magento_Theme/templates/html/topmenu.phtml – Saravanan DS Jan 30 at 10:46
  • check my answer – Rakesh Donga Jan 30 at 10:54
0

we can check Customer is logged in or not by below way, Using Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct method.

<?php

namespace Rd\Customer\Block;

class Customerinfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
    }

    /*
     * return bool
     */
    public function getLogin() {
        return $this->httpContext->isLoggedIn();
    }
?>

Call getLogin function in template file,

<?php
$isLoggedin = $block->getLogin();
        if(!$isLoggedin) {
    ?>
        <li class="level0 nav-8 last level-top ui-menu-item" role="presentation"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account/login');?>" class="level-top ui-corner-all" id="ui-id-9" tabindex="-1" role="menuitem"><span><?php echo __("Login")?></span></a>
        </li>
    <?php
        }else{
    ?>
    <li class="level0 nav-8 last level-top ui-menu-item" role="presentation"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account');?>" class="level-top ui-corner-all" id="ui-id-9" tabindex="-1" role="menuitem"><span><?php echo __("My Account")?></span></a>
    <ul class="logout-wrap"><li class="" role="menuitem"><a href="<?php echo $storeManager->getStore()->getUrl('customer/account/logout');?>" class="level-top ui-corner-all" id="ui-id-10" tabindex="-1" role="menuitem"><span><?php echo __("Logout")?></span></a></span></li></ul>
    </li>
<?php
    }
?>
  • Still same issue. – Saravanan DS Jan 30 at 10:59
  • you have created block file and after run compile command ? because this code work for me – Rakesh Donga Jan 30 at 11:04
  • In home page alone i am getting same link. Other than that, in product detail and category pages are working. – Saravanan DS Jan 30 at 11:55

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.