How to Display Complex Error & Success Messages in Magento 2

How to Display Complex Error & Success Messages in Magento 2

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

Steps to Display Complex Error & Success Messages in Magento 2:

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’,
                        ]
                    );

Conclusion:

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!

Previous Article

How to Create Child Theme in Magento 2

Next Article

How to Add Custom Fields to Checkout Page in Magento 2

Write a Comment
  1. 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?

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨