0

How can I show the My Account in header after the welcome message after successfully logged in my website?

I have got help in displaying them on customer menu but now I want to show it in header instead of customer menu.

Please see the below screenshot.

enter image description here

3
  • You want to add My account link after this ? Welcom, sivakumar n! My Account Log Out. Something like this ? Oct 22, 2019 at 12:11
  • Yes @KishanSavaliya. I want to display it after the customer logged in
    – Siv
    Oct 22, 2019 at 12:13
  • Plz review this magento.stackexchange.com/a/124697
    – Shorabh
    Oct 22, 2019 at 12:16

1 Answer 1

1

You can simply add this line in your default.xml file

<move element="my-account-link" destination="mycustom4.div" after="header.links" />

After adding this line you need to make some CSS changes there.

Another solution

If you do not need My Account link for Guest user then you can use below code.

Add this lines in your default.xml file

<referenceBlock name="my-account-link" remove="true" />

<referenceContainer name="header.container">
    <block class="Vendor\Module\Block\Account\Link" name="my-account-link-custom" after="header.links" template="Magento_Theme::header/myaccount_custom.phtml" />
</referenceContainer>

And Create one Block file here

app/code/Vendor/Module/Block/Account/Link.php

Content for this file is ...

<?php
namespace Vendor\Module\Block\Account;

use Magento\Customer\Block\Account\SortLinkInterface;

class Link extends \Magento\Customer\Block\Account\Link
{
    protected $_customerUrl;
    protected $httpContext;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->_customerUrl = $customerUrl;
        $this->httpContext = $httpContext;
        parent::__construct($context, $customerUrl, $data);
    }

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

And you need to add template file here..

app/design/frontend/Vendor/Theme/Magento_Theme/templates/header/myaccount_custom.phtml

Content for this file is..

<?php if($block->getIsCustomerLoggedIn()): ?>
    <li>
        <a href="<?php echo $block->getHref(); ?>"><?php echo __("My Account"); ?></a>
    </li>
<?php endif; ?>

After changing this please run below commands

php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush

Hope this will help you!

21
  • Thanks. But it will display the My Account link even though user not logged in the site. Any idea?
    – Siv
    Oct 22, 2019 at 12:34
  • 1
    Okay Let me know Oct 22, 2019 at 14:11
  • 1
    Ok Thanks. I will do it and let you know.
    – Siv
    Oct 23, 2019 at 7:14
  • 1
    I have updated as per your suggestion and working great now! Thanks
    – Siv
    Oct 23, 2019 at 9:35
  • 1
    You're most welcome. :) Oct 23, 2019 at 9:36

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.