1

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

thx for help

2

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

Vendor/Module/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 '';
    }
}

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>

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.