1

How do I add a custom class to "Create account " header link

and how to rename it label as "Open an Account" in Magento 2

Please refer the attachments

enter image description here

enter image description here

Can anyone guide me to resolve this issue?

2 Answers 2

5

You need to add this in your default.xml file inside body tag

<referenceBlock name="register-link">
    <arguments>
        <argument name="label" xsi:type="string" translate="true">Open an Account</argument>
        <argument name="class" xsi:type="string" translate="true">test-class</argument>
    </arguments>
</referenceBlock>

Update :

Add this in default.xml file

<referenceBlock name="register-link">
    <arguments>
        <argument name="template" xsi:type="string">Vendor_Module::openaccount.phtml</argument>
        <argument name="label" xsi:type="string" translate="true">Open an Account</argument>
        <argument name="class" xsi:type="string" translate="true">new-account-class</argument>
    </arguments>
</referenceBlock>

Create new phtml file openaccount.phtml in your module/theme and add this content

<li class="<?= $block->escapeUrl($block->getClass());?>">
    <a href="<?= $block->escapeUrl($block->getHref());?>">
        <?= $block->escapeUrl($block->getLabel());?>
    </a>
</li>

For Updated answer I followed Marius answer with some changes.

Hope this will help you!

3

By default you cannot add attributes on the li element. Only on the a element inside it.
But there is a workaround. you can specify to the block that renders the "create account" link a template. In this template you can do whatever you want.

SO add this in one of your custom modules in view/frontend/layout/default.xml

<referenceBlock name="register-link" template="[Vendor]_[Module]::register.phtml" />

then create the file view/frontend/templates/register.phtml with this content:

<li class="my-awesome-class">
    <a href="<?= $block->escapeUrl($block->getHref());?>">
        <?= $block->escapeHtml(__('Open an Account'));?>
    </a>
</li>
1
  • Hi @Marius, works fine
    – Jaisa
    Commented Dec 12, 2019 at 12:41

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.