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?
You can do this by pluginize exceute method. Try following way:
<?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>
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.
app/code/MyVendor/Mymodule
but it's not working. Please suggest if I am going wrong.
Commented
May 5, 2017 at 5:59
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>