tags:

views:

95

answers:

4

How do I retrieve a success message in Magento?

Array
(
    [core] => Array
        (
            [_session_validator_data] => Array
                (
                    [remote_addr] => 192.168.151.102
                    [http_via] => 
                    [http_x_forwarded_for] => 
                    [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4
                )

            [session_hosts] => Array
                (
                    [technova2] => 1
                )

            [messages] => Mage_Core_Model_Message_Collection Object
                (
                    [_messages:protected] => Array
                        (
                        )

                    [_lastAddedMessage:protected] => Mage_Core_Model_Message_Success Object
                        (
                            [_type:protected] => success
                            [_code:protected] => Your review has been accepted for moderation
                            [_class:protected] => 
                            [_method:protected] => 
                            [_identifier:protected] => 
                            [_isSticky:protected] => 
                        )

                )

            [just_voted_poll] => 
            [visitor_data] => Array
                (
                    [] => 
                    [server_addr] => -1062692990
                    [remote_addr] => -1062693018
                    [http_secure] => 
                    [http_host] => technova2
                    [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4
                    [http_accept_language] => en-US,en;q=0.8
                    [http_accept_charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
                    [request_uri] => /~rahuls/sextoys/index.php/review/product/list/id/169/
                    [session_id] => 21bq2vtkup5m1gtghknlu1tit42c6dup
                    [http_referer] => http://technova2/~rahuls/sextoys/index.php/review/product/list/id/169/
                    [first_visit_at] => 2010-06-16 05:49:56
                    [is_new_visitor] => 
                    [last_visit_at] => 2010-06-16 06:00:00
                    [visitor_id] => 935
                    [last_url_id] => 23558
                )

            [last_url] => http://technova2/~rahuls/sextoys/index.php/review/product/list/id/169/
        )     
)

After posting the review I want to display the message: "Your review has been accepted for moderation". It appears in the $_SESSION array, but how do I fetch it? Please help. Thanks in advance.

A: 

Since the message is stored in the core sub-array, you'll use the Magento core message block to retrieve it. In your layout, you should be able to see this line (in page.xml):

<block type="core/messages" name="global_messages" as="global_messages"/>

This means that the page calls the message block and retrieves messages from that core array. Then, in your layouts, you should be able to see the line that actually invokes the output:

<?php echo $this->getChildHtml('global_messages') ?>

This actually echos the normal message blocks for any messages in the session. If you cannot find those blocks, add them in. If you need to get the message in a different context (this may interfere with other site operation), try this in a phtml file:

<?php print $this->getLayout()->createBlock('core/messages')->toHtml(); ?>

Hope that helps!

Thanks, Joe

Joseph Mastey
A: 

Thanks for your Answer Joe. But it didn't work for me :( I found the below block in page.xml

I used the below two lines in my code for success message but no success.

getChildHtml('global_messages') ?> getLayout()->createBlock('core/messages')->toHtml(); ?>

Raul
A: 

It seems that what you are requesting already exists in Magento. Once the user has posted a review about a product, the "Your review has been accepted for moderation" message appears by default as stated in app/code/core/Mage/Review/controllers/ProductController.php at line 188 (Magento 1.4.0.1)

Anyway, if you want to display messages (notice, success, error, warning) just use, for instance in case of a success message:

<?php
$message = $this->__('Your success message here');
Mage::getSingleton('core/session')->addSuccess($message);
?>

The message will be store in the session and automatically appear on the frontend as long as the template file of the page has the $this->getMessagesBlock()->getGroupedHtml() code which is the case in all default phtml pages shipped with Magento. So you don't really have to bother.

Of course, in the above example, you can change

addSuccess($message)

by addError($message) or addWarning($message) or addNotice($message) depending of the kind of info your want to display.

vrnet
A: 
$messages = Mage::getSingleton('core/session')->getMessages(true);
foreach($messages->getItems() as $message)
{
   // Do something   
   $message->getText();
}
Lee