1

I am developing a newsletter extension. I try to override the template

/vendor/magento/module-customer/view/frontend/templates/account/dashboard/info.phtml

and the block Magento\Customer\Block\Account\Dashboard\Info to replace the Newsletter block with my own.

enter image description here

How can I do this? Thanks in advance.

  • Do you want to override only this selected section? Am I right here ? drive.google.com/file/d/1E9JxJ7glqSXNDi2TrtZAonc_OyrQ7QJO/… – Kishan Savaliya Oct 22 '19 at 10:26
  • Yes, thats true. But I think I am forced to override "Contact information" too since they are in the same template. – Black Oct 22 '19 at 10:28
  • 1
    Please check my answer. If it will helpful to you then you can approve my answer, and if you want more help then you can ask me. :) – Kishan Savaliya Oct 22 '19 at 10:42
1

You can create new module and override that file otherwise you do that using custom theme as well.

I'll show you here how can we override template file using custom module.

Please create Vendor name directory first, and move into that and create another directory Module.

Then you need to create registration.php file here.

app/code/Vendor/Module/registration.php

Content for this file is ..

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

After that please create one more directory there etc here and add module.xml file there..

app/code/Vendor/Module/etc/module.xml

Content for this file is ..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0" />
</config>

After that please create view directory there and move into that and create frontend move into that.

create layout directory and add customer_account_index.xml this file there..

app/code/Vendor/Module/view/frontend/layout/customer_account_index.xml

Content for this file is ..

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="customer_account_dashboard_info">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Vendor_Module::account/dashboard/info.phtml</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

After creating this file please go back once and create templates directory here

app/code/Vendor/Module/view/frontend/templates

and add info.phtml file here in this path

app/code/Vendor/Module/view/frontend/templates/account/dashboard/info.phtml

Content for this file is ..

<div class="block block-dashboard-info">
    <div class="block-title"><strong><?= $block->escapeHtml(__('Account Information')) ?></strong></div>
    <div class="block-content">
        <div class="box box-information">
            <strong class="box-title">
                <span><?= $block->escapeHtml(__('Contact Information')) ?></span>
            </strong>
            <div class="box-content">
                <p>
                    <?= $block->escapeHtml($block->getName()) ?><br>
                    <?= $block->escapeHtml($block->getCustomer()->getEmail()) ?><br>
                </p>
                <?= $block->getChildHtml('customer.account.dashboard.info.extra'); ?>
            </div>
            <div class="box-actions">
                <a class="action edit" href="<?= $block->escapeUrl($block->getUrl('customer/account/edit')) ?>">
                    <span><?= $block->escapeHtml(__('Edit')) ?></span>
                </a>
                <a href="<?= $block->escapeUrl($block->getChangePasswordUrl()) ?>" class="action change-password">
                    <?= $block->escapeHtml(__('Change Password')) ?>
                </a>
            </div>
        </div>
        <?php if ($block->isNewsletterEnabled()) : ?>
            <div class="box box-newsletter">
                <strong class="box-title">
                    <span><?= $block->escapeHtml(__('Newsletters')) ?></span>
                </strong>
                <div class="box-content">
                    <p>
                        <?php if ($block->getIsSubscribed()) : ?>
                            <?= $block->escapeHtml(__('You are subscribed to "General Subscription".')) ?>
                        <?php else : ?>
                            <?= $block->escapeHtml(__('You aren\'t subscribed to our newsletter.')) ?>
                        <?php endif; ?>
                    </p>
                </div>
                <div class="box-actions">
                    <a class="action edit" href="<?= $block->escapeUrl($block->getUrl('newsletter/manage')) ?>"><span><?= $block->escapeHtml(__('Edit')) ?></span></a>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

In this template file you can change anything whatever you want there.

You can override your block here

For that you need to create di.xml file here

app/code/Vendor/Module/etc/di.xml

Content for this file is ...

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Account\Dashboard\Info" type="Vendor\Module\Block\Account\Dashboard\Info" />
</config>

Then after you need to create Info.php file here

app/code/Vendor/Module/Block/Account/Dashboard/Info.php

Content for this file is ..

<?php

namespace Vendor\Module\Block\Account\Dashboard;

use Magento\Framework\Exception\NoSuchEntityException;

class Info extends \Magento\Customer\Block\Account\Dashboard\Info
{
    public function myCustomFunction(){
        return true;
    }
}

Here I've created my custom function and override default Info.php file we do not need to copy whole file just extend parent class and you can create your custom functions there.

Hope this will help you!

  • Thank you. It worked. This shows how to override the template. But the info on how to override the block is missing. E.g. what if I want to change or add a method in the block class Magento\Customer\Block\Account\Dashboard\Info ? – Black Oct 22 '19 at 10:50
  • 1
    Yes, I'm adding that within few mins – Kishan Savaliya Oct 22 '19 at 10:50
  • Please check updated answer, If it helps you, you can approve my answer. :) – Kishan Savaliya Oct 22 '19 at 10:52
  • 1
    Thank you very much sir, your solution is really good explained! Problem solved :) – Black Oct 22 '19 at 10:59
  • 1
    It's Okay not an issue. – Kishan Savaliya Oct 22 '19 at 11:07
3

Create app/code/Vendor/Module/view/frontend/layout/customer_account_index.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" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Account\Dashboard\Info" name="customer_account_dashboard_info" as="info" template="Vendor_Module::account/dashboard/info.phtml" cacheable="false"/>        
        </referenceContainer>
    </body>
</page>

Override Block in app/code/Vendor/Module/Block/Account/Dashboard/Info.php. Override functions from below file

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\Block\Account\Dashboard;

class Info extends \Magento\Customer\Block\Account\Dashboard\Info
{
}

Override template in app/code/Vendor/Module/view/frontend/templates/account/dashboard/info.phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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