0

Use codes:

1:

$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

                    <?php if($isLoggedIn){ // login menu ?> 
                        <a href="<?php echo $this->getUrl("customer/account/");?>">My Account <i class="fas fa-user ml-2"></i></a>
                        <a href="<?php echo $this->getUrl("customer/account/logout");?>">Logout <i class="fas fa-power-off ml-2"></i></a>
                    <?php }else{ // non login user ?>
                        <a href="<?php echo $this->getUrl('online-dealers'); ?>">Online Dealers <i class="fas fa-globe ml-2"></i></a>
                        <a href="<?php echo $this->getUrl('dealer-locator'); ?>">Dealer locator <i class="fas fa-map-marker-alt ml-2"></i></a>
                        <a href="<?php echo $this->getUrl("customer/account/login");?>">Dealer Login <i class="fas fa-user ml-2"></i></a> 
                        <a href="<?php echo $this->getUrl('customer/account/create');?>">Become A Dealer <i class="fas fa-thumbs-up ml-2"></i></a>
                        <?php } ?>

2:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) { // login user menu  }

When I log in with the no.1 code it doesn't show the login user menu for the first time, after one time hard refresh shows the login user menu. So no need for a hard refresh after customer login.

1
  • Hi @JaduDev did you able to resolve this issue. I am facing same issue Mar 1, 2023 at 7:46

2 Answers 2

0

First solution is near to your solution.But you have to access Magento\Framework\App\Http\Context value using injection instead of data object.

$context = $ObjectManager->get('Magento\Framework\App\Http\Context'); $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

You have to access class by [View model][1] . Create a view class and class Magento\Framework\App\Http\Context object.

 <?php

namespace {ClassNameSpace};

class {ClassName} implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
   private $context;

    public function __construct(\Magento\Framework\App\Http\Context $context)
    {
        $this->context = $context;
    }   
    public function isLogin()
    {
      return $this->context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    }
}

You can access at block like $viewModel = $block->getViewModel();

View class look this. You can access [1]: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html

2
  • I have created viewmodel same as above you mention but not solved my issue when I will login dealer first it redirect to customer page and show correct menu but when i open homepage first it show non login menu but when i refresh then it will show correct login menu.
    – JaduDev
    Apr 11, 2022 at 7:42
  • I also print the $helper->isLoggedIn() value the first time on the homepage it doesn't show any value but after one time refresh then it had show 1 as a value.
    – JaduDev
    Apr 11, 2022 at 8:10
0

This will be better approach

just create a helper class with below code,

<?php
namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private $httpContext;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        parent::__construct($context);
        $this->httpContext = $httpContext;
    }

    public function isLoggedIn()
    {
        $isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        return $isLoggedIn;
    }
}

Now It is not recomended to call helper directly in to phtml file so you can call this helper in your block or else you can use it like this in you template file

<?php
$helper = $this->helper('Vendor\Module\Helper\Data');

if ($helper->isLoggedIn()) {
    //do something here
}

else{
    //do something here
}
2
  • I have already created helper same as above you mention but not solved my issue when I login dealer first it redirect to customer page and show correct menu but when i open homepage first it show non login menu but when i refresh then it will show correct login menu.
    – JaduDev
    Apr 11, 2022 at 7:54
  • I also print the $helper->isLoggedIn() value the first time on the homepage it doesn't show any value but after one time refresh then it had show 1 as a value.
    – JaduDev
    Apr 11, 2022 at 8:10

Your Answer

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

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