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 ).
2 Answers 2
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.
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>
-
in header.html i see <?php echo $this->getChildHtml("top.links"); ?> where is path to edit top links fileuser1799722– user17997222018年10月09日 19:33:18 +00:00Commented Oct 9, 2018 at 19:33
My account?