5

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

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
    }
    ?>
  • could you specify the folder path and file name to put this code.. – aravind Nov 24 '17 at 10:00
  • This is not working as expected since it creating cache issue,after login it will always show login link on home page. – akgola Jul 4 '18 at 10:52
  • Bad form to use the object manager directly, inject the session dependency into your block. – siliconrockstar Oct 2 '18 at 15:53
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>
  • Thank you but i'm working on magento 2 – vnnogile_user Jul 7 '16 at 13:20
  • This is magento 2 code. – Aaron Allen Jul 7 '16 at 14:48
  • As i am new to magento 2.0 . could you please assist me through live example..I'm not getting @Aaron – vnnogile_user Jul 8 '16 at 7:41
  • You'll have to create a simple module. The tutorial here alanstorm.com/magento_2_mvvm_mvc will give you the tools to implement this. You'll just need the block, template, and a default.xml layout file to position the block. – Aaron Allen Jul 8 '16 at 9:42
3

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.

  • I know that it's coming from there but i'm not getting answer. – vnnogile_user Jul 8 '16 at 4: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 9 '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 7:44
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.

  • 2
    did you posted an answer before question posted ? – Baby in Magento Jul 7 '16 at 11:52
  • I am not getting. Actually i am using custom theme which having 3 links my account ,create account and sign in. but i want to show my account after successfully login. @BabyinMagento – vnnogile_user Jul 7 '16 at 11:54
0

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

I hope this helps..

Regards Gaurav

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.