1

I've create a custom module in Magento and I want to show notice message in admin when the observer fire so my module code like below :

etc/Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MI_ProductAutoSynchronization>
            <version>0.0.1</version>
        </MI_ProductAutoSynchronization>
    </modules>
    <global>
        <blocks>
            <MI_ProductAutoSynchronization>
                <class>MI_ProductAutoSynchronization_Block</class>
            </MI_ProductAutoSynchronization>
        </blocks>
        <helpers>
            <MI_ProductAutoSynchronization>
                <class>MI_ProductAutoSynchronization_Helper</class>
            </MI_ProductAutoSynchronization>
        </helpers>
        <models>
             <MI_ProductAutoSynchronization>
                <class>MI_ProductAutoSynchronization_Model</class>
             </MI_ProductAutoSynchronization>
        </models>
        <events>
            <mi_productautosynchronization_notifications_before>
                <observers>
                    <mi_productautosynchronization_observer>
                        <type>singleton</type>
                        <class>MI_ProductAutoSynchronization_Model_Observer</class>
                        <method>checkMessages</method>
                    </mi_productautosynchronization_observer>
                </observers>
            </mi_productautosynchronization_notifications_before>
        </events>
    </global>
    <default>
        <MI_ProductAutoSynchronization>
          <cron_time_update>0 0 * * *</cron_time_update>
        </MI_ProductAutoSynchronization>
    </default>
    <crontab>
        <jobs>
            <MI_ProductAutoSynchronization>
                <schedule>
                  <config_path>productautosynchronization_options/section_one/cron_time_update</config_path>
                </schedule>
                <run>
                  <model>MI_ProductAutoSynchronization/observer::test</model>
                </run>
            </MI_ProductAutoSynchronization>
        </jobs>
    </crontab>
    <adminhtml>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <productautosynchronization_options>
                                            <title>Custom Configuration Section</title>
                                        </productautosynchronization_options>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <MI_ProductAutoSynchronization>
                    <file>mi_productautosynchronization/mi_productautosynchronization.xml</file>
                </MI_ProductAutoSynchronization>
            </updates>
        </layout>
    </adminhtml>
</config>

etc/System.xml

<?xml version="1.0"?>
<config>
    <tabs>
        <customconfig translate="label" module="MI_ProductAutoSynchronization">
            <label>Product Auto Synch Tab</label>
            <sort_order>1000002</sort_order>
        </customconfig>
    </tabs>

    <sections>
        <productautosynchronization_options translate="label" module="MI_ProductAutoSynchronization">
            <label>Configuration Settings</label>
            <tab>customconfig</tab>
            <frontend_type>text</frontend_type>
            <sort_order>1000002</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <section_one translate="label">
                    <label>Product Time Update</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <cron_time_update translate="label">
                          <label>Products Time Update</label>
                          <frontend_type>select</frontend_type>
                          <source_model>MI_ProductAutoSynchronization/options</source_model>
                          <sort_order>1</sort_order>
                          <show_in_default>1</show_in_default>
                          <show_in_website>1</show_in_website>
                          <show_in_store>1</show_in_store>
                          <comment>Select the time to update stock automatically.</comment>
                        </cron_time_update>
                    </fields>
                </section_one>
            </groups>
        </productautosynchronization_options>
    </sections>
</config>

Block/Adminhtml/Notification.php

<?php
class MI_ProductAutoSynchronization_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
{
    public function _toHtml($className = "notification-global")
    {
        // Let other extensions add messages
        Mage::dispatchEvent('mi_productautosynchronization_notifications_before');
        // Get the global notification object
        $messages = Mage::getSingleton('mi_productautosynchronization/notification')->getMessages();
        $html = null;
        foreach ($messages as $message) {
            $html .= "<div class='$className'>" . $message . "</div>";
        }
        return $html;
    }
}

Model/ Notification.php

<?php
class MI_ProductAutoSynchronization_Model_Notification extends Varien_object
{
    protected $messages = [ ];

    public function getMessages()
    {
        return $this->messages;
    }

    public function setMessages($messages)
    {
        $this->messages = $messages;
        return $this;
    }

    public function addMessage($message)
    {
        $this->messages[] = $message;
        return $this;
    }
}

Model/Observer.php

<?php
class MI_ProductAutoSynchronization_Model_Observer {

    public function test() {
      Mage::log("TEST success", null, "dev.log");
    }

    public function checkMessages($observer)

      Mage::log("notification success", null, "dev.log");
      $notifications = Mage::getSingleton('mi_productautosynchronization/notification');
      $notifications->addMessage("I was sent by mi_productautosynchronization");
      return $observer;
    }
}

So My Question is: How can I show notice message like screen below when test function in observer fire ?

enter image description here

0

config.xml update this :

<events>
    <mi_productautosynchronization_notifications_before>
        <observers>
            <mi_productautosynchronization_observer>
                <type>singleton</type>
                <class>MI_ProductAutoSynchronization_Model_Observer</class>
                <method>checkMessages</method>
            </mi_productautosynchronization_observer>
        </observers>
    </mi_productautosynchronization_notifications_before>
</events>

to :

<events>
    <mi_productautosynchronization_notifications_before>
        <observers>
            <mi_productautosynchronization_observer>
                <type>singleton</type>
                <class>MI_ProductAutoSynchronization_Model_Observer</class>
                <method>checkMessages</method>
            </mi_productautosynchronization_observer>
            <mi_productautosynchronization_test_observer>
                <type>singleton</type>
                <class>MI_ProductAutoSynchronization_Model_Observer</class>
                <method>test</method>
            </mi_productautosynchronization_test_observer>
        </observers>
    </mi_productautosynchronization_notifications_before>
</events>

Observer.php

<?php
class MI_ProductAutoSynchronization_Model_Observer extends Varien_Event_Observer {

public function test($observer) {
      /*Display the message in the next refresh*/
      Mage::getSingleton('adminhtml/session')->addError(Mage::helper('MI_ProductAutoSynchronization')->__('Error sending Email'));

      // Refresh and display
      // $controllerAction = $observer->getEvent()->getControllerAction();
      // Mage::log("TEST success", null, "dev.log"); // for your logs
      // Mage::getSingleton('adminhtml/session')->addError(Mage::helper('MI_ProductAutoSynchronization')->__('Error sending Email'));
      // Mage::app()->getResponse()->setRedirect($controllerAction->getUrl('*/sales_order/')); //the controller where you want to redirect
      // Mage::app()->getResponse()->sendResponse();
      // exit ; 

}

public function checkMessages($observer) {
      Mage::log("notification success", null, "dev.log");
      $notifications = Mage::getSingleton('mi_productautosynchronization/notification');
      $notifications->addMessage("I was sent by mi_productautosynchronization");
      return $observer;
}
}

How the session messages work ?

addSuccess, addError, addNotice, addWarning.

The message that you give for those methods are stored in the session. when a page is viewed, Magento checks in the session for messages. If there are any, they are displayed and removed from the session, all the messages are saved to the session and are shown the next time.

| improve this answer | |
  • Sorry i've update my screenshot in my question i want this notice message in admin not in frontend. – mahmoudismail Jul 14 '17 at 8:33
  • please look my update – PЯINCƏ Jul 14 '17 at 8:42
  • i've got this error Fatal error: Call to a member function getEvent() on null in /MI/ProductAutoSynchronization/Model/Observer.php on line 8 – mahmoudismail Jul 14 '17 at 8:44
  • look again now. – PЯINCƏ Jul 14 '17 at 9:04
  • Sorry but it's not working. – mahmoudismail Jul 14 '17 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.