How to get a successful message in Magento?

How to get 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 publishing the review, I want to display a message: "Your review has been accepted for moderation." It appears in the $ _SESSION array, but how to get it? Please help. Thanks in advance.

+6
magento
source share
7 answers

This is a combination of all your answers. This works for me from just about any block:

  //A Success Message Mage::getSingleton('checkout/session')->addSuccess("Your cart has been updated successfully!"); //A Error Message Mage::getSingleton('checkout/session')->addError("Your cart has been updated successfully!"); //A Info Message (See link below) Mage::getSingleton('checkout/session')->addNotice("This is just a FYI message..."); //These two lines are required to get it to work session_write_close(); //THIS LINE IS VERY IMPORTANT! $this->_redirect('checkout/cart'); 

Credit at the expense of:

http://www.magentocommerce.com/boards/viewthread/40324/ (Where I posted the answer)

and

http://www.deepcodeonline.com/blog/magento/how-to-display-error-success-and-notice-messages-in-magento/

+9
source share

The following code works for me:

  • set the message to the controller:

     Mage::getSingleton('customer/session') ->addSuccess(Mage::helper('mymodule')->__('Data saved.')); 
  • init message in the controller where you want to receive the message:

     $this->loadLayout(); $this->_initLayoutMessages('customer/session'); $this->_initLayoutMessages('catalog/session'); $this->renderLayout();` 
  • Extract the message in the template file ( .phtml ):

     echo $this->getMessagesBlock()->getGroupedHtml(); 
+9
source share
 $messages = Mage::getSingleton('core/session')->getMessages(true); foreach($messages->getItems() as $message) { // Do something $message->getText(); } 
+6
source share

It seems that what you are requesting already exists in Magento. After the user has sent feedback about the product, the message "Your feedback was accepted for moderation" appears by default, as indicated in the application / code / kernel / Mage / Reviews / Controllers / ProductController.php on line 188 (Magento 1.4. 0.1)

In any case, if you want to display messages (notification, success, error, warning), just use, for example, in case of a successful message:

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

The message will be stored in the session and automatically appear in the external interface if the page template file has the code this this this this this this this this this this this this this is this this this this this this this this this is the this this this this this this is this this this is this this is get this.border :() this-> getMessagesBlock () β†’ getGroupedHtml (), which takes place in all standard phtml pages comes with Magento. Therefore, you really do not need to worry.

Of course, in the example above you can change

 addSuccess($message) 

by addError($message) or addWarning($message) or addNotice($message) depending on the type of information you want to display.

+5
source share

Since the message is stored in the submatrix core , you will use the main Magento message block to retrieve it. In your layout you should see this line (in the page.xml file):

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

This means that the page calls the message block and retrieves the messages from this main array. Then in your layouts you should see a line that actually causes output:

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

This is actually an echo of the normal message blocks for any messages in the session. If you cannot find these blocks, add them. If you need to receive a message in a different context (this may interfere with the operation of another site), try this in the phtml file:

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

Hope this helps!

Thanks Joe

+3
source share

Suppose you want to receive a successful message from a directory session:

Then you had to get messages from the session as follows:

 $messages = Mage::getSingleton('catalog/session')->getMessages(false); 

Putting false will not clear your collection of messages from the session. If you want to delete all messages after extraction, use true instead.

$messages is of type Mage_Core_Model_Message_Collection . You can get the necessary type of messages from it:

 $successMessages = $messages->getItemsByType(Mage_Core_Model_Message::SUCCESS); 

You can scroll through these messages with:

 foreach ($successMessages as $message) { //do whatever you like } 
+3
source share

I found the following block in page.xml

I used two lines in my code for a successful message, but did not succeed.

getChildHtml ('global_messages')? > getLayout () β†’ createBlock ('core / messages') β†’ toHtml () ;? >
+2
source share

All Articles