Magento : How do i echo username

Magento : How do i echo username

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : How do i echo username

Magento: Displaying Customer Name and Email in a Custom Template

In Magento, displaying customer information like name and email address within a custom template requires careful handling of the customer context. Let’s dive into how you can achieve this using modern theme templates.

Understanding the Customer Context

When working with customer data in a front-end template, it is crucial to understand the context:

  • If no customer is logged in or the session has expired, Magento treats the $this->getCustomer() method as if it were an empty object.
  • Calling methods on this ‘empty’ object results in a Fatal error: Call to a member function getFirstname() on a non-object exception.

To avoid this, you need to first check if the customer is logged in before attempting to access their attributes.

Checking for Customer Login Status

Before trying to echo the customer’s name or email, always verify if they are actually logged in:

<?php
  $customer = Mage::getSingleton('customer/session');
  if ($customer->isLoggedIn()) {
    // Proceed with customer data retrieval and display
  } else {
    // Handle guest user scenario (e.g., show a login link)
  }
?>

Retrieving Customer Name and Email

Assuming the customer is logged in, you can now safely retrieve their name and email address:

<?php $customer = Mage::getSingleton('customer/session'); ?>

<!-- Display customer name -->
<p>Welcome, <?php echo $this->htmlEscape($customer->getCustomer()->getName()) ?></p>

<!-- Display customer email -->
<p>Email: <?php echo $this->htmlEscape($customer->getCustomer()->getEmail()) ?></p>

Escaping Output for XSS Protection

Note the use of <?php $this->htmlEscape() ?> in the above examples. Always escape customer-provided data to protect against Cross-Site Scripting (XSS) attacks.

Replacing Placeholders in URLs

To replace placeholders like <!CUSTOMER NAME!!> and <!!!CUSTOMER EMAIL!!> in your live chat button URL, you can use a combination of string replacement and the customer’s attributes:

<!-- LiveChat Button with Customer Information -->
<a href="http://xxxxxx.fr/livezilla.php?code=BOUTIQUE&en=<?php echo urlencode($customer->getCustomer()->getName()) ?>&ee=<?php echo urlencode($customer->getCustomer()->getEmail()) ?>">
  Chat Support
</a>

Remember to use urlencode() for the customer’s name and email to ensure the URL is properly encoded.

Conclusion

Displaying customer information in a Magento template requires careful handling of the customer context. Always verify the login status before accessing customer attributes, escape output data for XSS protection, and use proper encoding when embedding values into URLs.

For more advanced techniques on working with customers and sessions in Magento, I recommend checking out this detailed guide by Laravel Company: Managing Customer Sessions in Magento.

By following these best practices, you can effectively display customer name and email information within your custom templates while ensuring the security of your application.

Themes Email Customers Errors

Our network