Question

I'm trying to get global messages in compare products page, but none of the messages get rendered, instead all of them appear in any subsequent page I'm going, after producing messages with for example removing product from comparsion. I understand that this is normal behaviour for Magento, because normally compare page has a popup template, but I setTemplate to 1column.phtml in my local.xml. Now I need global messages to appear there too. I included:

<?php echo $this->getMessagesBlock()->getGroupedHtml(); ?>

to catalog/product/compare/list.phtml, but messages only appear in any subsequent page and not in compare list page. As I found out from here, you can add custom messages. So I did

$this->getMessagesBlock()->addError('Test Error!');
echo $this->getMessagesBlock()->getGroupedHtml();

Now this custom error gets rendered as it should, but none of the other messages show. How could I get all session messages to be shown in compare page?

Was it helpful?

Solution

For messages to appear you need to call this in the controller before calling renderLayout()

$this->initLayoutMessages('catalog/session');

This will tell Magento to display the messages set for a particular session type. The example above tells Magento to display the messages set for the catalog session.
Other options are customer/session, core/session, checkout/session, tag/session and adminhtml/session and admin/session (for backend).
You can call this method with an array of parameters

$this->initLayoutMessages(array('catalog/session', 'customer/session'));

In conclusion, if you want messages displayed in the compare list you should override the product compare controller or use the event controller_action_layout_render_before_catalog_product_compare_index and init the session messages you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top