1

I want to change page title of My Account Page only like below:

welcome back, Customer.firstname()

How I can change my account page title only with customer firstname?

2 Answers 2

2

You can do this by pluginize exceute method. Try following way:

  1. Create plugin[Vendor/Module/etc/frontend/di.xml]

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\Index">
        <plugin name="Vendor_Module::my_account" type="Vendor\Module\Plugin\Customer\Controller\Account\Index" sortOrder="10"/>
    </type>
</config>
  1. Now create class [Vendor/Module/Plugin/Customer/Controller/Account/Index.php]

namespace Vendor\Module\Plugin\Customer\Controller\Account;

class Index
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @param \Magento\Customer\Model\Session $sessionSession
     */
    public function __construct(
        \Magento\Customer\Model\Session $sessionSession
    ) {
        $this->customerSession = $sessionSession;
    }

    public function afterExecute(
        \Magento\Customer\Controller\Account\Index $subject,
        $resultPage
    ) {
        $resultPage->getConfig()->getTitle()->set(__('Welcome back %1', $this->customerSession->getCustomer()->getName()));

        return $resultPage;
    }
}

Clear cache.

9
  • I have tried the above code, I have created the same files and directory structure in the app/code/MyVendor/Mymodule but it's not working. Please suggest if I am going wrong. Commented May 5, 2017 at 5:59
  • Sorry, I have just tried to format you answer with the color code. Please ignore it. Commented May 5, 2017 at 6:10
  • Did you create module? that means 'Vendor\Module'?
    – Sohel Rana
    Commented May 5, 2017 at 8:01
  • Check here ranasohel.me/2014/03/07/creating-a-custom-module-in-magento-2, after that follow this answer.
    – Sohel Rana
    Commented May 5, 2017 at 8:03
  • Verndor => your vendor name, Module => your module name, Got It?
    – Sohel Rana
    Commented May 5, 2017 at 8:04
0

I would like to complete @Sohel Rana answer. In the magento version I use (2.4.5-p1) I had to set the block title to empty. Otherwise it will always take the title set in layout instead of the controller.

in your theme folder : app/design/frontend/Vendor/theme/Magento_Customer/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">
  <body>
    <referenceBlock name="page.main.title">
      <action method="setPageTitle">
        <argument translate="true" name="title" xsi:type="string"></argument>
      </action>
    </referenceBlock>
  </body>
</page>

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.