How to display success and error messages in Magento 2

- Fire development, Magento 2

Magento 2 display success and error messages

In Magento 2, you can effortlessly display both success and error messages, providing users with feedback or additional information regarding what they do. In the following article, we describe where this feature is usually applied. Besides, you will find out how to display success and error messages as well as customize them

'

Where to display success and error messages in Magento 2

By default, the platform lets you display success and error messages in Magento 2 for both store visitors and administrators. You can inform users on frontend and backend about the completeness of their actions. 

How to display success and error messages in Magento 2

In this article, we describe two ways of showing success and error messages in Magento 2:

  • messageManager;
  • Observer.

Both approaches provide the fastest way to to keep in contact with users, even if your module has no frontend or backend views. A message you want to show appears in a dedicated block every time it is refreshed via a page load or an ajax call. Let’s describe how to display success and error messages in Magento 2 with the help of messageManager.

messageManager

The messageManager object is responsible for messaging in Magento 2. It means that you need to get its instance to write messages. The preferred way is the following: you have to inject this class into your class by adding the following snippet to your __construct method:

Now, you can call one of these messages:

Observer

Now, let’s find out how to display success and error messages with the help of the observer. However, there is no way to show messages if you are on an observer. And if you call your observer method during an AJAX call, the corresponding message block doesn’t reload. 

Luckily, you can by-pass the limitation by throwing an error. Thus, an error message will be displayed. For instance, you can use the following code:

The corresponding message is displayed on the frontend since it is the result of your AJAX request. Test it with the sales_order_save_before observer.

How to customize success and error messages in Magento 2

Now, let’s take a look at how to customize success and error messages in Magento 2. Since we’ve described to ways of displaying them, this chapter also consists of two sections: 

messageManager

In the previous chapter, we described how to call one of the following messages using messageManager:

To customize them, you have to replace Error, Warning, Notice, and Success with a new message.

Observer

As for customizing success and error messages in Magento 2 created with the help of observers, you have to replace “Prices have been changed!” with your own text:

Conclusion

As you can see, there is nothing sophisticated about displaying messages in Magento 2. Furthermore, you can easily customize them reaching both store visitors and administrators in the fastest way. Display only the most essential information at the right time.  For further information, check this article: Magento 2 display success and error messages. You can find more tips on how to work with Magento 2 here: Magento 2 Developer’s Cookbook.

'