0

How can i pass variable to observer from controller? Or is there a way to wait that execute observer before execute method in controller?

2

try this code

Config.xml

<global>
  <events>
    <my_custom_event>
      <observers>
        <namespace_modulename_my_custom_event_observer>
          <type>singleton</type>
          <class>modulename/observer</class>
          <method>my_custom_method</method>
        </namespace_modulename_my_custom_event_observer>
      </observers>
    </my_custom_event>
  </events>
</global>

Use this code in controller file

<?php
..
$event_data_array  =  array('cid' => '123');
Mage::dispatchEvent('my_custom_event', $event_data_array);
..
?>

use this code in observer file

<?php
class Namespace_Modulename_Model_Observer
{
  public function my_custom_method($observer) {
    $event = $observer->getEvent();

    // getter method to fetch varien object passed from the dispatcher
    $varien_object = $event->getVarienObj();
    $varien_object->setCid('456');
  }
}
?>

OR

You can also refer below links to get the idea how them works

https://code.tutsplus.com/tutorials/custom-events-in-magento-with-the-observer-pattern--cms-22120

http://inchoo.net/magento/custom-magento-events-customer-first-order/

http://www.demacmedia.com/magento-commerce/mini-tutorial-dispatching-your-own-event-in-magento/

  • why $varien_object->setCid('456') ? where is the variable passed? – Altravista Oct 11 '18 at 13:58
  • if you want update variable(cid) value then you can update in observer file. – Balwant Singh Oct 11 '18 at 14:06
  • but i want to get value from controller not update it. i try with this code $cid = $varien_object->getCid(); but get error – Altravista Oct 11 '18 at 14:11
  • can you add your error here? – Balwant Singh Oct 11 '18 at 14:11
  • Call to a member function getCid() on a non-object – Altravista Oct 11 '18 at 14:12

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.