5

Homepage how to show my account link in header after successfully login in magento

vnnogile_user
  • 385
  • 3
  • 4
  • 17

5 Answers5

6

Use the following code to show your account link in header:

<?php
    $objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManagerlogin->get('Magento\Customer\Model\Session');
    $baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
?>
    <?php if($customerSession->isLoggedIn()) { ?>
   <a href="<?php echo $baseurl .'customer/account/logout'; ?>">LOGOUT</a>
    <?php }else { ?>
        <a href="<?php echo $baseurl .'customer/account/login'; ?>">LOGIN</a>       
        <?php
    }
    ?>
Abhinav Singh
  • 2,592
  • 15
  • 14
5

Take a look at the places where Magento does this already. If you take a look at the customer modules default.xml:

vendor/magento/module-customer/view/frontend/layout/default.xml

You will see a call to the code that creates the sign in link:

<referenceBlock name="top.links">
    <block class="Magento\Customer\Block\Account\Link" name="my-account-link">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">My Account</argument>
        </arguments>
    </block>
    <block class="Magento\Customer\Block\Account\RegisterLink" name="register-link">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">Create an Account</argument>
        </arguments>
    </block>
    <block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link"
           template="account/link/authorization.phtml"/>
</referenceBlock>

I'm pointing out the AuthorizationLink class in this code. This makes a call to a template file that has the logic that fills in the conditions for having a link that changes based on if the user is logged in or not.

/** @var $block \Magento\Customer\Block\Account\AuthorizationLink */
$dataPostParam = '';
if ($block->isLoggedIn()) {
    $dataPostParam = sprintf(" data-post='%s'", $block->getPostParams());
}
?>
<li class="authorization-link" data-label="<?php echo $block->escapeHtml(__('or')); ?>">
    <a <?php /* @escapeNotVerified */ echo $block->getLinkAttributes(); ?><?php /* @escapeNotVerified */ echo $dataPostParam; ?>>
        <?php echo $block->escapeHtml($block->getLabel()); ?>
    </a>
</li>

While is isn't exactly what you are looking to do (magento always does it's work in the most convoluted way possible), it's a way in the theme to add in blocks that call in templates that will do the kind of work you are looking for.

circlesix
  • 4,273
  • 3
  • 27
  • 57
  • I know that it's coming from there but i'm not getting answer. – vnnogile_user Jul 08 '16 at 04:53
  • There is no way for anyone without that theme to help you with the exact code that you need to get this functionality. The theme is creating a block or container in it's xml for those links, using that block or container to hook into and add your link is the only way to get a template in there that does what you want. Most likely the file driving that is {{theme}}/Magneto_Theme/layout/default.xml but this only a guess. – circlesix Jul 09 '16 at 15:22
  • @circlesix Do you know how the authorization.phtml will call firstly and check that customer is logged in or not? – Rutvee Sojitra Aug 20 '18 at 07:44
3

Here's an example block:

use Magento\Customer\Model\Url;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Template;

class Links extends Template
{
    /** @var Url $_customerUrl */
    protected $_customerUrl;

    /** @var Context $httpContext */
    protected $httpContext;

    /**
     * Links constructor.
     * @param Template\Context $context
     * @param array $data
     * @param Url $customerUrl
     * @param Context $httpContext
     */
    public function __construct(Template\Context $context,
                                Url $customerUrl,
                                Context $httpContext,
                                array $data)
    {
        $this->_customerUrl = $customerUrl;
        $this->httpContext = $httpContext;

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

    /**
     * Is logged in
     *
     * @return bool
     */
    public function isLoggedIn()
    {
        return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    }

    /**
     * Get account URL
     * 
     * @return string
     */
    public function getAccountUrl()
    {
        return $this->_customerUrl->getAccountUrl();
    }
}

And how you might call it from template:

<ul class="header links">
    <?php if ($block->isLoggedIn()): ?>
    <li>
        <a href="<?php echo $block->getAccountUrl() ?>">
            <?php echo __('My Account') ?>
        </a>
    </li>
    <?php endif; ?>
</ul>
Aaron Allen
  • 9,009
  • 2
  • 26
  • 36
0

There is a setting in the backend to redirect the customer to his dashboard page after logging in.
Look for it in System->Configuration->Customer configuration.

Marius
  • 197,939
  • 53
  • 422
  • 830
0

In magento 2, when you sign In then create an account link is already gone.

I hope this helps..

Regards Gaurav