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

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

5 hours ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 hours ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 hours ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago