Hi,
In our website, We have My Account link in the top right section in our theme. It displays for logged in users and guest users both. But we need to display My Account link for logged in users only.
How to change that? Where to change it?
Please help to fix that.
Thanks
Below is a small plugin using a method similar to what's used in core for adding/removing the "Register" top link in
Magento\Customer\Block\Account\RegisterLink
File: Vendor/Module/Plugin/Link.php <?php namespace Vendor\Module\Plugin; use Magento\Customer\Model\Context; class Link { protected $httpContext; public function __construct( \Magento\Framework\App\Http\Context $context ) { $this->httpContext = $context; } public function afterToHtml(\Magento\Customer\Block\Account\Link $subject, $result){ if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) { return $result; } return ''; } }
File: Vendor/Module/etc/frontend/di.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Customer\Block\Account\Link"> <plugin name="vendor_module_toplinks_myaccount" type="Vendor\Module\Plugin\Link" sortOrder="10" /> </type> </config>
Hi @jeganee
You can use this in your phtml file to check condition if customer logged in or not:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->get('Magento\Customer\Model\Session'); if($customerSession->isLoggedIn()) {
// Add your "Myaccount" Link here } else {
// You can give here "Login / Register" Link
}
I hope it will solve your problem.