Skip to main content

My Account display only after login

If Want to know how to display "My Account" only after customer LogIn. After LogOut it will again be hidden on the same place.

The customer_logged_* layout handles used in M1 were removed in M2. 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

Pawan/Myaccount/Plugin/Link.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 '';
    }
}

Pawan/Myaccount/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="pawan_myaccount_toplinks_myaccount" type="Pawan\Myaccount\Plugin\Link" sortOrder="15" />
    </type>
</config>

Comments

Post a Comment