1

I've seen multiple types of messages on magento 2. Error messages, success messages and so on. How can I get the total number of messages that magento can provide? It should be sum of messages displayed on each and every scenario.

How can I get that exact amount?

2

You can check all available messages in Magento, Check below file

vendor/magento/framework/Message/ManagerInterface.php

List of messages in Magento

addMessage

addMessages

addError

addWarning

addNotice

addSuccess

addErrorMessage

addWarningMessage

addNoticeMessage

addSuccessMessage

addComplexErrorMessage

addComplexWarningMessage

addComplexNoticeMessage

addComplexSuccessMessage

addUniqueMessages

addException

addExceptionMessag

| improve this answer | |
0

Mostly we need below messages, that you can display for viewing purpose like this

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$messageManager = $objectManager->create('\Magento\Framework\Message\ManagerInterface');
$messageManager->addError('This is Error Message');
$messageManager->addSuccess('This is Success Message');
$messageManager->addNotice('This is Notice Message');
$messageManager->addWarning('This is Warning Message');
?>
| improve this answer | |
0

Using below code you can get the all current session messages

use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Message\Success;
use Magento\Framework\Message\Error;
use Magento\Framework\Message\Warning;
use Magento\Framework\Message\Notice;

class Message
{
/**
 * @var ManagerInterface
 */
private $messageInterface;

/**
 * Message constructor
 * @param ManagerInterface $messageInterface ManagerInterface
 */
public function __construct(
    ManagerInterface $messageInterface
) {
    $this->messageInterface = $messageInterface;
}//end __construct()

public function execute()
{
    $messages     = $this->messageInterface->getMessages(false);
    $messageArray = [];
    foreach ($messages->getItems() as $message) {
        if ($message instanceof Success) {
            $messageArray['success'][] = $message->getText();
        }
        if ($message instanceof Error) {
            $messageArray['error'][] = $message->getText();
        }
        if ($message instanceof Warning) {
            $messageArray['warning'][] = $message->getText();
        }
        if ($message instanceof Notice) {
            $messageArray['notice'][] = $message->getText();
        }
    }
    if (isset($messageArray['success'])) {
        $messageArray['success'] = array_unique($messageArray['success']);
    }
    if (isset($messageArray['error'])) {
        $messageArray['error'] = array_unique($messageArray['error']);
    }
    if (isset($messageArray['warning'])) {
        $messageArray['warning'] = array_unique($messageArray['warning']);
    }
    if (isset($messageArray['notice'])) {
        $messageArray['notice'] = array_unique($messageArray['notice']);
    }

    print_r($messageArray);
     }//end execute()
}//end class
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.