How To

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!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

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?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

8 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago