how to show my account link in header after successfully login in magento
-
I am Using magento custom theme.they giving 3 links create account, sign in and my account. i would like to show my account after successfully log-in.– vnnogile_userCommented Jul 7, 2016 at 11:56
-
Duplicate of magento.stackexchange.com/questions/124878/…– themanwhoknowsthemanCommented Sep 23, 2016 at 7:59
-
Possible duplicate of How can i check whether user is logged in or not in magento 2.0– Rakesh DongaCommented Sep 19, 2019 at 6:48
5 Answers
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..– aravindCommented Nov 24, 2017 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.– akgolaCommented Jul 4, 2018 at 10:52
-
1Bad form to use the object manager directly, inject the session dependency into your block. Commented Oct 2, 2018 at 15:53
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. Commented Jul 8, 2016 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. Commented Jul 9, 2016 at 15:22 -
@circlesix Do you know how the authorization.phtml will call firstly and check that customer is logged in or not? Commented Aug 20, 2018 at 7:44
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>
-
-
1
-
As i am new to magento 2.0 . could you please assist me through live example..I'm not getting @Aaron Commented Jul 8, 2016 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. Commented Jul 8, 2016 at 9:42
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
-
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 Commented Jul 7, 2016 at 11:54
In magento 2, when you sign In then create an account link is already gone.
I hope this helps..
Regards Gaurav