0

I want to show customer first name and last name after logged in Magento2.2.5? As default magento fetches the first name and last name after logged in.

In my case, I have removed magento default first name and last name and added custom first name and last name for some customization purpose.

After adding my custom fields, the first name and last name is not shown in header after customer logged in Magento2.2.5?

Please someone guide me on this.

Any help would be appreciated.

Thanks in advance.

  • Why & where from you remove First name & Last name ? from code, database etc..? – Chirag Patel May 1 at 11:39
0

Get customer session and display name of customer using below code.
Add below code in your file.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

<?php if($customerSession->isLoggedIn()):?>
    <a href="<?php echo $this->getUrl('customer/account') ?>">Welcome! <?php 
       echo $customerSession->getCustomer()->getName(); ?>
    </a>
0

You need to do following things.

  1. create your block file with following code :

    protected $_customerSession;
    
    public function __construct(\Magento\Customer\Model\Session $customerSession) {
    $this->_customerSession = $customerSession;
    }
    
    public function getCustomerInfo()
    {
        return $this->_customerSession->getCustomer(); 
    }
    
  2. Now call the code into your phtml file like this.

    <?php
    $customerdata = $block->getCustomerInfo();
    echo $customerdata->getCustomFirstNameField();
    echo $customerdata->getCustomLastNameField();
    ?>
    

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.