1

enter image description here

Hi, I have created custom theme. I want change account page title from "My Dashboard" to loggedin customer name. How I can do this.

Although I have found file customer_account_index.xml that is setting title. But no idea how to make dynamic.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
    <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">My Dashboard</argument>
        </action>
    </referenceBlock>
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="customer_account_dashboard_top" as="top"/>
        <block class="Magento\Customer\Block\Account\Dashboard\Info" name="customer_account_dashboard_info" as="info" template="account/dashboard/info.phtml" cacheable="false"/>
        <block class="Magento\Customer\Block\Account\Dashboard\Address" name="customer_account_dashboard_address" as="address" template="account/dashboard/address.phtml" cacheable="false"/>
    </referenceContainer>
</body>

Thanks in advanced!!!!

4 Answers 4

7

You can also use this below code.

customer_account_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
    <referenceBlock name="page.main.title" template="Vendor_Module::template.phtml">
    </referenceBlock>
</body>
</page>

template.phtml

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()):
?>
<h1 class="page-title">
    <span class="base" data-ui-id="page-title-wrapper"><?php echo($customerSession->getCustomer()->getName()); ?></span>
</h1>
<?php endif; ?>
0
5

In this case, we can use layout_generate_blocks_before

On this fire an observer which change title and set Current customer name in place of My Dashboard

Observer:

<?php
namespace Devamitbera\ChangePagetitle\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverChangePagetitleForAccount implements ObserverInterface
{


    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $_actionFlag;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;


    protected $_pageConfig;
    protected $customerSession;

    /**
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
         \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\View\Page\Config $pageConfig     
    ) {
        $this->_actionFlag = $actionFlag;
        $this->messageManager = $messageManager;
        $this->redirect = $redirect;
        $this->_pageConfig = $pageConfig; 
        $this->customerSession = $customerSession;
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /** @var \Magento\Framework\App\Action\Action $controller */
        $full_action_name = $observer->getFullActionName();

        $layout = $observer->getEvent()->getLayout();
        if($full_action_name == 'customer_account_index'){
            //print_r($layout->getBlock('head')->getRobots());
            //$this->_pageConfig->setRobots('NOINDEX,NOFOLLOW');

            $layout->getBlock('page.title')->setPageTitle($this->customerSession->getCustomer()->getName());
        }

    }

}
2

You need to override Magento/Customer/Controller/Account/Index.php

It should be like,

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

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    protected $customerSession;
    public function __construct(
        Context $context,
        \Magento\Customer\Model\Session $customerSession,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    /**
     * Default customer account page
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('My Account')); //for browser tab title
        $this->_view->getLayout()->getBlock('page.main.title')->setPageTitle($this->customerSession->getCustomer()->getName()); //dynamic user name as a title
        return $resultPage;
    }
}

I have tested it works!

1

Please follow below steps

1) Find this file

root\app\design\frontend\Namespace\themeName\Magento_Customer\layout\customer_account_index.xml

=>> Please comment 'My Dashboard' title referenceBlock.

2) Then Find this file

root\app\design\frontend\Namespace\themeName\Magento_Customer\templates\account\dashboard\info.phtml

Put below 2 lines on top after php tag start:

$userNameObj = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $userNameObj->get('Magento\Customer\Model\Session');

Then add below code after block 'block-dashboard-info' class div :

   <div class="page-title-wrapper">
        <h1 class="page-title">
    <?php if($customerSession->isLoggedIn()) {
        $userName = $customerSession->getCustomer()->getName()."<br/>";  // get  Full Name ?>
            <span class="base" data-ui-id="page-title-wrapper">Welcome <?php echo $userName; ?></span>
            <?php 
          }  ?>
          </h1>
    </div>

Now, login from frontend it will display "Welcome user user".

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.