How To

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!

Click to rate this post!
[Total: 10 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago