How to Add Customer Name in CMS Page/Block in Magento 2

How to Add Customer Name in CMS Page Block in Magento 2

Hello Magento Friends,

Welcome to the “How To” Magento 2 Blog series. Today’s topic to learn about is How to Add Customer Name in CMS Page/Block in Magento 2. 

Introduction:

74% of people feel frustrated if the website content is not personalized and stops them from shopping. Hence, customization helps in boosting the user experience of your store and enhance sales. With Magento 2, you can customize everything from the look of the website to product categories and their features.

With Magento 2, you can customize CMS Page or Block. Magento merchants can add customer names to provide a personalized experience to customers. Let’s start learning How to Add Customer Name in CMS Page/Block in Magento 2.

Steps to Add Customer Name in CMS Page/Block in Magento 2:

Step 1: Create one block file on your custom extension at the below path:

app\code\Vendor\Extension\Block\Customerinfo.php

<?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();
           }
      }  
}

Step 2: Now create a phtml file in the below path 

app\code\Vendor\Extension\view\frontend\templates\mycustomfile.phtml

<?php echo $block->getCurrentCustomernameinfo(); ?>

Now call this phtml file into your CMS Block or CMS Page.

{{block class="Vendor\Extension\Block\Customerinfo" template="mycustomfile.phtml"}}

Conclusion:

This way you can Add Customer Name in CMS Page/Block in Magento 2. For any hardships with the execution of the above code, write to me in the comment part below. Do share the article amongst your Magento colleagues. Stay tuned for more such Magento solutions.

Happy Coding!

Previous Article

Reward Points AKA Customer Loyalty Points For Magento 2 (Ft. Best Rewards Points Systems)

Next Article

How to Add Filterable Product Attribute in Products in Category in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨