Question

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

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top