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.
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.
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"}}
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!
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
If you are a Shopify admin, using a Shopify Balance Account for your business revenue…
Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…
Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…