5

I am trying to display a customer's name after login (M2 version)

Question: How do I display a customer's name after login in Magento 2?

I see here it is a little more difficult: Customer name is not display after login in magento 2

4 Answers 4

1

I customised to login logout and it is working well but I need customer name two thing I used for customised

1) link.php

2) authorization.phtml

I have such code in link.php:

if (false != $this->getTemplate()) {
     return parent::_toHtml();
     }
     return '<div><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></div>';
    }
1
  • i need name of customer with this please help
    – dipak
    Commented Oct 5, 2018 at 7:48
10

There are two way

  1. Using object manager into your phtml file:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->create('Magento\Customer\Model\Session');
    
    if ($customerSession->isLoggedIn()) {
        $customerSession->getCustomerId();  // get Customer Id
        $customerSession->getCustomerGroupId();
        $customerSession->getCustomer();
        $customerSession->getCustomerData();
    
        echo $customerSession->getCustomer()->getName();  // get  Full Name
       echo $customerSession->getCustomer()->getEmail(); // get Email
    }
    
  2. Construct customer session class into your relevant block and create a function to get Customer name. Then call your block function from your phtml file.

2
  • Awesome, thanks :) Commented Mar 25, 2018 at 19:50
  • Method 1 may make website slow. Better to use the method 2
    – Ask Xah
    Commented Aug 20, 2019 at 11:21
6

Use the below function in your helper file with Magento\Customer\Model\Session to get session object in $customerSession

public function getLoggedinCustomerName(){

        if($customerSession->getData('customer_id'))
        {
            $customer = $this->_customerRepositoryInterface->getById($customerSession->getData('customer_id'));
            return $customer->getFirstname();
        }
        else
        {
            return "Guest";
        }
    }

and in use it in your PHTML file

<?php
$customerName = $helper->getLoggedinCustomerName();
?>
 <a href="<?php echo $block->getUrl('customer/account/'); ?>">
                <i class="fa fa-user" aria-hidden="true"></i>
                <span class="label"><?php /* @escapeNotVerified */ echo 'Hello '. $customerName; ?></span>
            </a>
5
  • Using Helper is a better options. Good Answer
    – Ask Xah
    Commented Aug 20, 2019 at 11:20
  • @AskBytes Use of helper classes are deprecated in 2.3 , Please opt for viewModel implementation . this is quite old answer . Please let me know if you need more info. Cheers !! Commented Aug 21, 2019 at 5:09
  • Yes, i need a solution on viewmodel implementation.
    – Ask Xah
    Commented Aug 21, 2019 at 6:46
  • Hi, Please share the viewModel implementation that will be greatly Appreciated.
    – Ask Xah
    Commented Aug 21, 2019 at 8:22
  • @AskBytes please gave look at this article markshust.com/2019/03/13/… Commented Aug 22, 2019 at 4:23
0

Php Block --

$customer =  $this->customerRepository->getById($this->customerSession->getCustomerId()); 


echo $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Name')->setObject($customer)->toHtml()

Some case: Bug of Magento 2 when you turn on caches. You must reload customer sections when customer logged in. Check etc/sections.xml and customer-data.js you will know how to reload customer sections. Regards.

I will update related link later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.