How do i add custom class to “Create account ” header link and how to rename it label as “Open an Account” in magento 2

magento.stackexchange https://magento.stackexchange.com/questions/298518

Domanda

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?

È stato utile?

Soluzione

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!

Altri suggerimenti

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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top