Magento function getMessages - how to detect if messages has been set

Magento function getMessages - how to detect if messages has been set

September 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento function getMessages - how to detect if messages has been set

Detecting Set Messages in Magento

As a seasoned developer working on e-commerce projects, I often find myself needing to check if any messages have been set in Magento, especially when dealing with cache invalidation. The getMessages() function is the key to this, but it can be tricky to use correctly.

Let’s start by understanding what the getMessages() method actually does. Located in core/session, it retrieves all saved messages and optionally clears them:

public function getMessages($clear=false)

The $clear parameter is a boolean value that determines whether the messages should be cleared after being retrieved.

Checking for Set Messages

Now, to check if any messages have been set, we need to call this method without clearing them. Here’s an example of how to do this:

// Get all messages but don't clear them
$messages = $this->getMessages(false);

// Check if there are any messages
if (!empty($messages)) {
    // Messages have been set
}

In the code above, we’re calling getMessages() with false as the second parameter. This retrieves all current messages without clearing the session.

We then use PHP’s empty() function to check if the $messages array is empty or not. If it contains any items (i.e., messages), empty() will return false, indicating that we have set messages.

Validating Cache Based on Messages

As you mentioned, one common use case for this technique is invalidating cache when a message is present. Here’s an example of how you might implement this:

// Get all messages but don't clear them
$messages = $this->getMessages(false);

// Check if there are any messages
if (!empty($messages)) {
    // Clear the specified cache key(s)
    Mage::app()->cleanCache(['your_cache_key_here']);

    // Alternatively, you can use a cache tag
    Mage::app()->getCacheInstance()->cleanTag('YOUR_CACHE_TAG');
}

In this example, if any messages are found in the session, we clear the specified Magento cache using either a key or a tag. This ensures that our cache is always up-to-date with any new data from the message.

Conclusion

Checking for set messages in Magento involves calling getMessages() without clearing the messages and then checking if the returned array is empty or not. This technique is invaluable when you need to invalidate cache based on the presence of session messages.

Remember, proper cache invalidation is crucial for maintaining accurate data in your e-commerce application. Regularly reviewing and updating your caching strategy can significantly improve performance and accuracy.

For more advanced tips on Magento development and performance optimization, I recommend checking out our Magento Development Guide at Laravel Company. There you’ll find a wealth of resources to help you master the platform.

Happy coding!

Cache

Our network