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: 2 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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago