0

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.

2
  • Can you describe in more details, where you want to added this one? Jan 15, 2021 at 6:15
  • In a CMS page or block. The user should see his own name, for example "Hello Mr Vasoya"
    – Black
    Jan 15, 2021 at 8:59

1 Answer 1

1
  1. 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();
        }
     }  
    }
    
  2. now in create custom phtml file : mycustomfile.phtml

    <?php echo $block->getCurrentCustomernameinfo(); ?>
    
  3. Now call this phtml file into you CMS Block or CMS Page.

    {{block class="Vendor\Extension\Block\Customerinfo" template="mycustomfile.phtml"}} 
    
2
  • Thank you! Where do I have to store the template file to make it work?
    – Black
    Jan 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

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.