I need to add the name of the customer into a CMS Site or Block. I tried to insert {{var customer.name}}
as seen in the documentation, but it does not work.
-
Can you describe in more details, where you want to added this one?– Dhiren VasoyaJan 15, 2021 at 6:15
-
In a CMS page or block. The user should see his own name, for example "Hello Mr Vasoya"– BlackJan 15, 2021 at 8:59
1 Answer
create one block file on your custom extension like this :
<?php namespace Vendor\Extension\Block class Customerinfo extends \Magento\Framework\View\Element\Template { protected $_session; public function __construct(\Magento\Customer\Model\Session $session) { ... $this->_session = $session; ... } public function getCurrentCustomernameinfo() { if ($this->_session->isLoggedIn()) { return $this->_session->getCustomer()->getName(); } } }
now in create custom phtml file :
mycustomfile.phtml
<?php echo $block->getCurrentCustomernameinfo(); ?>
Now call this phtml file into you CMS Block or CMS Page.
{{block class="Vendor\Extension\Block\Customerinfo" template="mycustomfile.phtml"}}
-
Thank you! Where do I have to store the template file to make it work?– BlackJan 15, 2021 at 11:22
-
You need to store the phtml into your extension into path : vendor\extension\view\frontend\templates Jan 15, 2021 at 11:29