cancel
Showing results for 
Search instead for 
Did you mean: 

Display logged in customer name in a page or block luma theme

SOLVED

Display logged in customer name in a page or block luma theme

https://magento.stackexchange.com/questions/153452/how-to-display-customer-name-when-logged-in-magen...

I found above

Created a customer.phtml in magento root folder. And also in 

/vendor/magento/module-theme/view/frontend/templates/html/ directory

 

 

<?php
$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
}
?>

I don't know how to call customer.phtml. 

In mangento admin.  Content - pages - homepage -content, I tried these to

 

{{block type="core/template" template="customer.phtml"}} 

{{block class="Magento\Framework\View\Element\Template" template="customer.phtml"}} 

{{block class="Magento\Framework\View\Element\Template" template="magento_module-theme::customer.phtml"}} 
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Display logged in customer name in a page or block luma theme

Please follow the following steps:

- Create a new pltml file "customername.phtml" under this folder "vendor/magento/module-customer/view/frontend/templates"

It should be like : vendor/magento/module-customer/view/frontend/templates/customername.phtml

 

app/design/frontend/{{vendor_namespace}}/{{theme_name}}/Magento_Customer/templates/customername.phtml


For best approach you should create this file in your current theme.

- Then Admin->Content->Pages->"GO TO YOUR CURRENT HOMEPAGE"->Edit Page

Design->Layout Update XML

add following code:

<referenceContainer name="main">    
    <block class="Magento\Framework\View\Element\Template" name="customername" template="Magento_Customer::customername.phtml" />
</referenceContainer>

Save and flush cache.

Please see attachment for the reference.
sa.png

- Then check homepage.


I hope it will work for you!

View solution in original post

9 REPLIES 9

Re: Display logged in customer name in a page or block luma theme

Hi @nudylady 

You can enable template path and can check in which block or phtml file you need to show customer name. 

Then you can add following code in the particular template file.

<?php
$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
}
?>

To enable template path you can get more info from following link:
https://www.mageplaza.com/kb/how-to-enable-template-path-hints-magento-2.html

I hope it will help you.

Re: Display logged in customer name in a page or block luma theme

thank you

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/* @var $block \Magento\Cms\Block\Widget\Block */
$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
}
?>
<div class="widget block block-static-block">
    <?= /* @noEscape */ $block->getText() ?>
</div>

template hint shows the file is 

/vendor/magento/module-cms/view/frontend/templates/widget/static_block/default.phtml

The code above is my edited default.phtml.

what should I do next? I want to show the name in Page - Home page - content area.

Re: Display logged in customer name in a page or block luma theme

Please follow the following steps:

- Create a new pltml file "customername.phtml" under this folder "vendor/magento/module-customer/view/frontend/templates"

It should be like : vendor/magento/module-customer/view/frontend/templates/customername.phtml

 

app/design/frontend/{{vendor_namespace}}/{{theme_name}}/Magento_Customer/templates/customername.phtml


For best approach you should create this file in your current theme.

- Then Admin->Content->Pages->"GO TO YOUR CURRENT HOMEPAGE"->Edit Page

Design->Layout Update XML

add following code:

<referenceContainer name="main">    
    <block class="Magento\Framework\View\Element\Template" name="customername" template="Magento_Customer::customername.phtml" />
</referenceContainer>

Save and flush cache.

Please see attachment for the reference.
sa.png

- Then check homepage.


I hope it will work for you!

View solution in original post

Re: Display logged in customer name in a page or block luma theme

thank you I try now

Re: Display logged in customer name in a page or block luma theme

Did you flush the magento cache?

Please test once after cache cleared. It works for me.

Re: Display logged in customer name in a page or block luma theme

Finally it worked. Thank you very much.

Re: Display logged in customer name in a page or block luma theme

Hi, hope you can help me.

I want to apply this in a site Im working in, could you guide me?

 

Thanks!

Re: Display logged in customer name in a page or block luma theme

Hi,

 

The file name and directory have already changed to 'vendor/magento/module-customer/view/frontend/templates/account/customer.phtml'.

Would you tell me how to do it now?

Re: Display logged in customer name in a page or block luma theme

Tried but it not display name but the code that was in the phtml file that I made .

 

Capture.PNG