Robert Berkley
Robert Berkley

Reputation: 51

Why can I not get the message block in Magento to show on the category page?

I'm trying to get Magento to redirect after a customer clicks the 'Add to Cart' button over to the category page, which I have done with no problem (after a little bit of searching around, of course) by using a hidden field with the name of "return_url". This part works perfectly, and the item is added to the cart, and the user is redirected back to the category page. Once here, no matter what I've tried I cannot get the message block to show the success (or error) message. Here is my most recent code attempt (in view.phtml):

$messages=Mage::getSingleton("checkout/session")->getMessages();
echo $this->getLayout()->createBlock("core/messages")->setMessages($messages)->getGroupedHtml();

Thanks!

Upvotes: 5

Views: 6274

Answers (2)

Steven Fritzsche
Steven Fritzsche

Reputation: 9

You can add a message to different session model types like "catalog", "customer", "checkout" or simple "core".

Same as:

Mage::getSingleton('catalog/session')
Mage::getSingleton('customer/session')
Mage::getSingleton('checkout/session')
Mage::getSingleton('core/session')

In your case, the customer session messages are not initialized in the category controller. Take a look at

\Mage_Catalog_CategoryController::viewAction

The end of the function should look like this:

$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('checkout/session');
$this->renderLayout();

To display the messages from "customer/session", this area must look like this:

$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('checkout/session');
$this->renderLayout();

Upvotes: 0

Ben Lessani
Ben Lessani

Reputation: 2151

Have you tried more simple addSuccess/addError/addNotice functions?

Mage::getSingleton('core/session')->addError(Mage::helper('core')->__('An error'));
Mage::getSingleton('core/session')->addSuccess(Mage::helper('core')->__('A success'));
Mage::getSingleton('core/session')->addNotice(Mage::helper('core')->__('A notice'));

Upvotes: 3

Related Questions