Hello Magento Friends,
Today we will be learning How to Display Complex Error & Success Messages in Magento 2.
It is important to notify customers about actions they perform on your website. Magento 2 provides default success and error messages for the actions taken by users in the frontend as well as the backend. It uses default functions for showing different types of action messages in the form of text.
But if you want to show a custom message for failure or successful action like adding a link along with the text. This cannot be done with default functions. For that, you need to follow the below steps.
So, let’s start
Step 1: Go to the below path
app\code\Vendor\Extension\etc\frontend\di.xml
And add the below code
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool"> <arguments> <argument name="configurationsMap" xsi:type="array"> <item name="addCustomMessage" xsi:type="array"> <item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item> <item name="data" xsi:type="array"> <item name="template" xsi:type="string">Vendor_Extension::messages/addCustomMessage.phtml</item> </item> </item> </item> </argument> </arguments> </type> </config>
Step 2: Then move to the following path
app\code\Vendor\Extension\view\frontend\templates\messages\addCustomMessage.phtml
And add code as follows
<?= $block->escapeHtml(__( 'Please <a href="%1">click here</a> to redirect to your account.', $block->getData('url') ), ['a']); ?>
Step 3: Add a complex success or error message in your Controller action.
//Error Message $this->messageManager->addComplexErrorMessage( 'addCustomMessage', [ 'url' => ‘YOUR_URL’, ] ); //Success Message $this->messageManager->addComplexSuccessMessage( 'addCustomMessage', [ 'url' => ‘YOUR_URL’, ] );
Thus, you can easily add your custom error and success messages in Magento 2. If you need any other customizations for your Magento 2 store, Hire a Certified Magento Developer.
Add your questions in the comments if you have any.
Happy Coding!
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…
View Comments
Hi ,
Hi,
I tried it, worked for me but I am facing an issue with displaying an error message. I am getting empty messages. Can you please help me with where I am going wrong?
Kindly check Magento log file to get more idea.