Magento 2

Essential Guide to Displaying Success, Error, And Warning Messages in Magento 2

This blog post provides an essential guide to displaying success, error, and warning messages in Magento 2. It offers valuable insights into the importance of system messages and how they enhance user experience and aid in debugging. The post also delves into the usage of the messageManager interface in Magento 2, demonstrating how to add and customize different types of messages. Whether you’re a developer or a system administrator, this guide will equip you with the knowledge to effectively manage system messages in Magento 2.


What are Success, Error, and Warning Messages in Magento 2?

In Magento 2, system messages are feedback provided to the user based on their interactions with the site. They come in three types:

  • Error Messages: These messages are displayed when an operation fails or a process encounters an issue that prevents it from completing successfully. For example, if a user tries to add an out-of-stock item to the cart, an error message will be displayed.
  • Success Messages: These messages are displayed when an operation or process completes successfully. For example, when a user successfully adds an item to their cart, a success message is shown.
  • Warning Messages: These messages are displayed when there could be an issue that the user should be aware of, but it doesn’t necessarily stop a process from completing. For example, if a product is almost out of stock, a warning message might be displayed.
Success Error And Warning Messages in Magento 2

Why are they important?

System messages in Magento 2 are important for several reasons:

  • User Feedback: They provide immediate feedback to the user about their actions. This helps users understand whether their action was successful or not.
  • Guidance: These messages guide users on what to do next, especially when an error occurs. For example, if a user enters an incorrect password, an error message will guide them to enter the correct one.
  • Enhanced User Experience: Timely and appropriate system messages enhance the user experience. They make the site more user-friendly as users are kept informed about their actions.
  • Debugging: For developers and system administrators, these messages are helpful in debugging. They provide valuable insights into what went wrong when an issue occurs.

Using the `messageManager` Interface in Magento 2

In Magento 2, the Magento\Framework\Message\ManagerInterface interface to manage system messages.

For fetching the object of Magento\Framework\Message\ManagerInterface you need to instantiate an object in your __construct() method. Here’s an example:

public function __construct(
    \Magento\Framework\Message\ManagerInterface $messageManager
) {
    $this->messageManager = $messageManager;

This interface allows developers to add error, success, warning, and notice messages.

It’s important to note that the addError(), addSuccess(), addWarning(), and addNotice() methods are now deprecated.

Adding Success Messages

To add a success message, you can use the addSuccessMessage() function. Here’s an example:


Adding Error Messages

To add an error message, you can use the addErrorMessage() function. Here’s an example:


Adding Warning Messages

To add a warning message, you can use the addWarningMessage() function. Here’s an example:


Adding Notice Messages

To add a notice message, you can use the addNoticeMessage() function. Here’s an example:


Best practices for using `messageManager`

When using messageManager, keep the following best practices in mind:

  • User-Friendly Messages: Ensure that your messages are user-friendly and easy to understand. Avoid technical jargon that might confuse users.
  • Appropriate Message Type: Use the appropriate message type for each situation. For example, use error messages for operations that fail, and success messages for operations that complete successfully.
  • Actionable Error Messages: If an error occurs, provide an actionable message that helps users understand how to resolve the issue.

Customizing System Messages in Magento 2

Magento 2 provides several ways to customize system messages. Let’s explore how to change the appearance of messages and translate them.

Changing the appearance of messages

The appearance of system messages can be changed by modifying the CSS. Here’s an example of how to change the background color of success messages:

.message-success {
    background-color: #DFF0D8;
    border-color: #D6E9C6;
    color: #3C763D;

In this example, the message-success class is used to change the background color, border color, and text color of success messages.

 Translating Messages

Magento 2 supports internationalization, which allows you to translate system messages. You can use the __() function to translate messages. Here’s an example:


In this example, the __() function is used to translate the success message. You can provide translations for different languages in the i18n directory of your module.


In conclusion, Magento 2’s system messages play a crucial role in enhancing user experience and debugging. They provide immediate feedback, guide users, and aid developers. The messageManager interface allows developers to add and manage these messages. While using messageManager, it’s important to ensure messages are user-friendly and actionable. Magento 2 also allows customization of system messages, including changing their appearance and translating them for internationalization. Thus, effectively using system messages can significantly improve the usability and user-friendliness of a Magento 2 site.

To deepen your understanding on Magento 2, check out these valuable resources: Magento 2 Developer Documentation

Dive into the intricacies of Magento 2’s system.xml with our detailed guide, a must-read for anyone looking to master the platform’s configuration: Master Magento 2 system.xml

It’s my hope that this blog post has been of value to you.  If you have any doubts or questions, feel free to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *