cancel
Showing results for 
Search instead for 
Did you mean: 

How can i display success or error message into home page in custom theme layout ?

SOLVED

How can i display success or error message into home page in custom theme layout ?

Hello,

I need to display magento default error/success message into magento 2's Ves/Fasony theme layout file.

 

Please let me know how can i do that ?

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How can i display success or error message into home page in custom theme layout ?

Try this,

 

we need to assign a placeholder for the message through frontend layout xml used in the module as given below inside the <body> tag:

<referenceContainer name="page.messages">
        <block class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
        <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>
</referenceContainer>

The above layout update will make use of the magento's message template to display the messages.

 

Thanks!!

View solution in original post

8 REPLIES 8

Re: How can i display success or error message into home page in custom theme layout ?

Hello @yagnik_solanki

 

Messaging in Magento 2 is handled by a message manager object. So if you want to write messages, you need to get an instance of it. The prefered method is to inject this class into your class by adding this to your __construct method:

 

protected messageManager;

public function __construct(
...
\Magento\Framework\Message\ManagerInterface $messageManager,
...
){
$this->messageManager = $messageManager;
...
}

With this, you can call one of the following messages:

 

$this->messageManager->addError(__("Error"));
$this->messageManager->addWarning(__("Warning"));
$this->messageManager->addNotice(__("Notice"));
$this->messageManager->addSuccess(__("Success"));
 

If found my answer useful? Please give Kudos and accept it as solution.

Magento Developer
Ankita Biswas

Re: How can i display success or error message into home page in custom theme layout ?

Thanks Ankita for reply,

 

But i need to doing this using xml file not php code.

Re: How can i display success or error message into home page in custom theme layout ?

Try this,

 

we need to assign a placeholder for the message through frontend layout xml used in the module as given below inside the <body> tag:

<referenceContainer name="page.messages">
        <block class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
        <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>
</referenceContainer>

The above layout update will make use of the magento's message template to display the messages.

 

Thanks!!

Re: How can i display success or error message into home page in custom theme layout ?

Thanks,

I will try 

Re: How can i display success or error message into home page in custom theme layout ?

Hello

In Magento 2, we can achieve this in two steps:

First, we need to assign the message to 'messageManager' from your module as below:

$this-&gt;messageManager-&gt;addSuccess(__('This is a success message.'));
Secondly, we need to assign a placeholder for the message through frontend layout xml used in the module as given below inside the <HTML><HEAD></HEAD><BODY> tag:

<REFERENCECONTAINER name="page.messages">
<BLOCK class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"></BLOCK>
<BLOCK class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"></BLOCK>
</REFERENCECONTAINER>
The above layout update will make use of the magento's message template to display the messages.</BODY></HTML>

If found my answer useful? Please give Kudos and Accept it as Solution.
Magento Developer
Ankita Biswas

Re: How can i display success or error message into home page in custom theme layout ?

Thanks for reply,

 

It works well

Re: How can i display success or error message into home page in custom theme layout ?

Hi @yagnik_solanki,

 

Use this extension to effortlessly display custom messages on cart, checkout & order success pages with a call to action button and custom URL. Use any of the 6 layouts to display the message. 

Re: How can i display success or error message into home page in custom theme layout ?

it works on custom module where you work all custom things .