I would like to remove My Account link from top links, when customer is not logged in.

How do I achieve it via XML layout files?


You can do it without xml file also which will be easier for your task. First step that you have to do is override header part and once its done apply some simple logic like by checking cx is logged in or not.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {//apply logic here}
  • You shouldn’t use the object manager to do this. 1. It’s wrong. 2. Full page cache will get in the way with it being header related. – andy jones Dec 18 '19 at 6:41

You can remove via overriding theme layout or xml file.


<referenceBlock name="top.links">
    <referenceBlock name="register-link" remove="true" />           <!--for Create Account Link-->
    <referenceBlock name="my-account-link" remove="true" />         <!--for My Account Link-->

I hope this will help you

  • This will always remove My Account link, I need to remove it only when the customer is logged out – giani.sim Feb 8 '17 at 9:49
  • I need to display My Account link only for logged in customer – giani.sim Feb 8 '17 at 9:53

Okay, there are two ways to go about this, you can do it with XML but also at the same time you're able to also do this with a helper.

The First way through a helper requires modification of theme templates and also a module making for Magento.


Create your module, call it what you will And add a helper file into


Inside this file you can use the following:

namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
     * @var \Magento\Framework\App\Http\Context
    private $httpContext;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->httpContext = $httpContext;

    public function isLoggedIn()
        $isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        return $isLoggedIn;

This will then allow you in your frontend theme to use:

$helper = $this->helper('Vendor\Module\Helper\Data');

 echo 'Customer is logged in';

This won't be affected by full page cache and means you can add this when and where you need.

Second Option. The XML One.

Again you'll need to create a module for Magento, but this time you're going to create a module that adds XML handles to the page. This one is a little more complicated than using the helper.

So in this one, we're going to create an observer to add the handles to the page if the customer is either logged in or logged out.

Our first observer:


Will look like this:

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;

class AddHandles implements ObserverInterface

    /** @var CustomerSession  */
    protected $customerSession;

     * AddHandles constructor.
     * @param CustomerSession $customerSession
    public function __construct(
        CustomerSession $customerSession
    ) {
        $this->customerSession = $customerSession;

     * Determine if the customer is logged in and if so add a layout handle
     * @param Observer $observer
    public function execute(Observer $observer)
        $layout = $observer->getEvent()->getLayout();

        if ($this->customerSession->isLoggedIn()) {
        } else {

Then inside of:


We'll add the following to call the handles for the frontend.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_load_before">
    <observer name="add_layout_handles" instance="Vendor\Module\Observer\AddHandles" />

You'll then be able to use the xml files:



In your frontend veiw.

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.