0

enter image description hereI want different top header section for guest and login user.

Guest user header option

"Hi Guest" + sign-in option

Login user header option

"Hi [User Name]"

How do I perform it programmatically?

  • Are you using default magento 2 luma theme? And what is version of your magento? – Sweety Masmiya Jul 18 at 4:28
  • yes and using magento 2.3.2 with sample data – Naiwrita09 Jul 18 at 4:35
1

You could achieve this requirement using a Plugin.

File - Vendor\Module\etc\di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Theme\Block\Html\Header" type="\Vendor\Module\Block\Html\MyCustomHeader" />
</config>

File - \Vendor\Module\Block\Html\MyCustomHeader.php

<?php

namespace Vendor\Module\Block\Html;


class MyCustomHeader extends \Magento\Theme\Block\Html\Header
{
    protected $_template = 'Magento_Theme::html/header.phtml';
    protected $_customerSession ;

    public function __construct(\Magento\Customer\Model\Session $customerSession, \Magento\Framework\View\Element\Template\Context $context, array $data = []) 
    {
        $this->_customerSession = $customerSession ;

        parent::__construct($context, $data);
    }


    /**
     * Custom Logic For Welcome Text
     *
     * @return string
     */
    public function getWelcome()
    {
        if($this->_customerSession->isLoggedIn()) {
            return parent::getWelcome();
        }
        else {
            return __('Hi Guest!');
        }
    }
}
  • Thank you it worked. – Naiwrita09 Jul 19 at 4:05
  • Sir I have added an image. I want to know how to change the "Myaccount" to logged in user name? – Naiwrita09 Jul 19 at 6:00
0

Actual file path vendor/magento/module-theme/view/frontend/templates/html/header.phtml

Override header.phtml file app/design/frontend/vendore/theme/Magento_Theme/templates/html/header.phtml

After that change

$welcomeMessage = $block->getWelcome();

to

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   $welcomeMessage = $block->getWelcome();
} 
else{
    $welcomeMessage = 'Hi Guest!';
}

sign-in option automatically remove for login customer.

  • is this feasible while designing any other website using magento – Naiwrita09 Jul 18 at 4:49
  • Can you please elaborate your question? – Sweety Masmiya Jul 18 at 4:51
  • Suppose I am designing a website using magento 2 then will this change in code work for the same task – Naiwrita09 Jul 18 at 4:53
  • If you will create another website with Magento then also this code is usable for you. – Sweety Masmiya Jul 18 at 4:56
  • Ok thank you. and if I use the other theme and not luna? – Naiwrita09 Jul 18 at 5:11

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.