How to add Custom error message in magento

How to add Custom error message in magento

September 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add Custom error message in magento

Adding Custom Error Messages for Unauthenticated Users in Magento

As a Magento developer, you may often need to display custom error messages based on user authentication status. In this guide, we will walk through how to add a custom error message when an unauthenticated user clicks the “Track Your Order” link in the footer of your Magento store.

Understanding the Problem

When an unauthenticated user clicks the “Track Your Order” link in the footer, Magento should display a clear and informative message prompting them to log in or create an account before they can access their order information. The existing functionality might not provide this user-friendly experience out of the box.

Solution Approach

To solve this issue, we will follow these steps:

  1. Locate the “Track Your Order” link in your Magento theme’s footer layout.
  2. Create a custom error message template.
  3. Write a module to handle unauthenticated users and display the custom error message.
  4. Integrate our new functionality into the existing footer layout.

Implementing the Solution

First, navigate to your Magento theme’s directory (usually found under app/design/frontend/<Vendor>/<Theme>). Look for the file responsible for rendering the footer, often named footer.phtml.

Find the line of code that renders the “Track Your Order” link. It might look something like this:

<a href="<?php echo $this->getUrl('sales/order/history') ?>">Track your order</a>

Step 2: Creating a Custom Error Message Template

Create a new file named order_tracking_error.phtml inside the template directory of your theme. In this file, design an error message template that includes the required styling and content.

<div class="tracking-error-message">
    <p><strong>Please login to track your order</strong></p>
    <a href="<?php echo $this->getUrl('customer/account/login') ?>">Login</a>
</div>

Step 3: Writing the Custom Error Message Module

Create a new module in app/code/<Vendor>/OrderTrackingError. In the etc/frontend/events.xml file, define an observer to check if the user is authenticated before accessing the order history page.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/events.xsd">
    <event name="sales_order_history_index_prepare_layout">
        <observer name="check_authentication_for_tracking" instance="<Vendor>\OrderTrackingError\Observer\CheckAuthentication"/>
    </event>
</config>

Step 4: Integrating the Custom Error Message

In your footer.phtml file, add a conditional statement to include our custom error message template only when the user is not authenticated.

<a href="<?php echo $this->getUrl('sales/order/history') ?>" id="track-order-link">
    Track Your Order
</a>

<?php if (!Mage::helper('customer')->isLoggedIn() && $block->getRequest()->getModuleName() == 'sales'): ?>
    <?php echo $this->getLayout()->createBlock('<Vendor>\OrderTrackingError\Block\Message')->setTemplate('order_tracking_error.phtml')->toHtml(); ?>
<?php endif; ?>

Best Practices and Further Reading

  1. Always use Magento’s built-in Mage::helper() functions for authentication checks.
  2. Create clean, reusable blocks to display error messages.
  3. Follow Magento’s coding standards and best practices.

For more advanced customizations and a comprehensive overview of Magento development, I recommend checking out the official Magento Developer Documentation or browsing the extensive tutorials available on our blog at Laravel Company.

Conclusion

In this guide, we learned how to add a custom error message for unauthenticated users trying to access order tracking functionality in Magento. By following these steps and using Magento’s built-in features and best practices, you can enhance the user experience of your e-commerce store.

Remember to replace <Vendor> with your actual vendor name throughout the code examples. Happy coding!

Extensions Themes Orders Customers

Our network