1

i want to remove magento top link My Account if user is not logged in ,i do not want to modify core files, i tried to get solution but could not find any working one .

i tried below method in : "Magento_Customer/layout/default.xml"

<customer_logout>
    <referenceBlock name="top.links">
        <referenceBlock name="my-account-link" remove="true" />
    </referenceBlock>
</customer_logout>

but it gives Error :

Element 'customer_logout': This element is not expected. Expected is one of ( attribute, block, referenceBlock, referenceContainer, container, move, uiComponent ).

3
  • What is the Url of your My account ?
    – PЯINCƎ
    Commented Oct 7, 2018 at 16:37
  • example.com/customer/account left to login Commented Oct 7, 2018 at 16:39
  • by default magento only show sign in link. once a customer log in then only it will show my account
    – Pawan
    Commented Oct 7, 2018 at 17:32

2 Answers 2

1

There is no customer_logout layout handle in M2, so You can't do with xml, there is no more customer_logged_in, customer_logged_out like M1

Solution1: It's depends to your theme, try to enable the path hints, and find if you find the phtml who display "My Account" Then add if($block->customerLoggedIn()) condition arround that link.

Solution2: If your link is added via Xml, you have to create an observer with layout_load_before event, then you can add $layout->addHandle('customer_logged_in') something like:

$layout = $observer->getEvent()->getLayout();
if ($this->customerSession->isLoggedIn()) {
    $layout->getUpdate()->addHandle('customer_logged_in');
} else {
    $layout->getUpdate()->addHandle('customer_logged_out');
}

Check this link, you have a full solution.

0

you can't do it via layout instead you have to do it with knockout js / phtml file . Edit your theme header.phtml file in Magento_Theme/templates/html folder and add condition like this :

<li data-bind="scope: 'customer'">
<!-- ko if: customer().firstname  -->
<a href="<?php echo $this->getUrl('customer/account/'); ?>" style="display:none;" data-bind="style: {display:'inline'}">
    <span class="icon-account"><i>My Account</i></span></a>
</a>
<!-- /ko -->

<script type="text/x-magento-init">
{
"*": {
  "Magento_Ui/js/core/app": {
    "components": {
      "customer": {
        "component": "Magento_Customer/js/view/customer"
      }
    }
  }
}
}
</script>
</li>
1
  • in header.html i see <?php echo $this->getChildHtml("top.links"); ?> where is path to edit top links file Commented Oct 9, 2018 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.