0

I have been looking around on others solutions but I can't seem to find any fix with my issue.

I want to hide My Account link when your signed out. But I want to show it when your signed in. I have default.xml inside www\app\design\frontend\Vendor\Theme\Magento_Customer module that show My Account on signin and signout.

Is it possible to fix with .xml or do I have to create a header.phtml fix www\app\design\frontend\Vendor\Theme\Magento_Theme\templates\html?

I do have a custom theme with file default.xml

    <?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="top.links">
            <block class="Magento\Customer\Block\Account\Link" name="my-account-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Account</argument>
                    <argument name="sortOrder" xsi:type="number">110</argument>
                </arguments>
            </block>
            <block class="Magento\Customer\Block\Account\RegisterLink" name="register-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">Create an Account</argument>
                </arguments>
            </block>
            <block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link"
                   template="Magento_Customer::account/link/authorization.phtml"/>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Account\AuthenticationPopup" name="authentication-popup" as="authentication-popup" template="Magento_Customer::account/authentication-popup.phtml">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="authenticationPopup" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Customer/js/view/authentication-popup</item>
                                <item name="children" xsi:type="array">
                                    <item name="messages" xsi:type="array">
                                        <item name="component" xsi:type="string">Magento_Ui/js/view/messages</item>
                                        <item name="displayArea" xsi:type="string">messages</item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </block>
            <block name="customer.section.config" class="Magento\Customer\Block\SectionConfig"
                   template="Magento_Customer::js/section-config.phtml">
                <arguments>
                    <argument name="sectionNamesProvider" xsi:type="object">Magento\Customer\Block\SectionNamesProvider</argument>
                </arguments>
            </block>
            <block name="customer.customer.data"  class="Magento\Customer\Block\CustomerData"
                   template="Magento_Customer::js/customer-data.phtml"/>
            <block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
                   template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
        </referenceContainer>
        <referenceBlock name="sale.reorder.sidebar" remove="true"/>
    </body>
</page>

3 Answers 3

0

you can remove in theme add file Magento_Customer/layout/customer_account_login.xml or in module Vendor/Module/view/layout/customer_account_login.xml

<referenceBlock name="my-account-link" remove="true" />

2
  • I tried but it seems like it only applies to one page "Login page". When I switch to another page My Account Link returnes back.
    – i37Ow
    Feb 15, 2022 at 9:56
  • My Account show up everywhere on the website but I want it to show when your signed inn. Sorry for my bad explanation.
    – i37Ow
    Feb 15, 2022 at 10:55
0

Have you tried to create new ? Like this:

view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="my-account-link" remove="true" />
        <referenceBlock name="top.links">
            <block class="Magento\Customer\Block\Account\Link" name="my-account-link-new" before='-' template="MyVender_MyModule::my-account.phtml" />
        </referenceBlock>
    </body>
</page>

view/frontend/templates/my-account.phtml

<?php 
    $customerSession = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Customer\Model\Session::class);
    if($customerSession->isLoggedIn()):
?>
    <li>
        <a href="<?= $block->getHref(); ?>"><?= __("My Account"); ?></a>
    </li>
<?php endif; ?>

P.S. I used object manager for quick test only, its better to inject it on your construct so you need to either override Magento\Customer\Block\Account\Link or create new block

3
  • Hello it's working great but there is a issue I am having. Perhaps you know a solution. When I enter category or pages the My Account Link vanish? But it returns if I go to My account page or other account related pages.
    – i37Ow
    Feb 17, 2022 at 19:13
  • I think the php code dosn't match when your outside Magento/Customer/ location hmm...
    – i37Ow
    Feb 17, 2022 at 19:55
  • I see2, but I'm glad you found a solution. Feb 18, 2022 at 2:03
0

I updated my-account.phtml with this code and it works fine. The My Account link flash on page reload but it's ok it a script.

<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'}"><?= __("My Account"); ?></a>
<!-- /ko -->
<script type="text/x-magento-init">
{
"*": {
  "Magento_Ui/js/core/app": {
    "components": {
      "customer": {
        "component": "Magento_Customer/js/view/customer"
      }
    }
  }
}
}
</script>
</li>

Your Answer

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

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