Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
update picture
Source Link
mahmoudismail
  • 742
  • 2
  • 9
  • 34

enter image description hereenter image description here

enter image description here

enter image description here

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

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

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 :

So My Question : how i can show notice message like screen below when test function in observer fire ?

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 :

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

Source Link
mahmoudismail
  • 742
  • 2
  • 9
  • 34

Magento Observer Show Notice Message

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 : how i can show notice message like screen below when test function in observer fire ?

enter image description here