2

I want to call observer in controller using if condition this is my code

app\code\local\EPayment\EPayment\controllers\IndexController.php

class EPayment_EPayment_IndexController extends Mage_Core_Controller_Front_Action{
    public function getepaymentAction()
    {
        $params = $this->getRequest()->getParams();
        $left_amount = $params['left_amount'];
        if($left_amount == true){
          // call observer here
           $this->getResponse()->setHeader('Content-type', 'application/json');
          $this->getResponse()->setBody($left_amount);
        }
    }
}

app\code\local\EPayment\EPayment\Model\Observer.php

class EPayment_EPayment_Model_Observer
{
    public function setDiscount($observer)
    {
        // some codes here
    }
}

The above code $left_amount is true call Observer.php.

Note: I write code for set discount in Observer.php.

5
  • You Observer.php file is located inside Model folder ! Can you please confirm you want to use Model or Observer ?
    – MGento
    Dec 8, 2017 at 6:32
  • @MGento: Thanks for reply i want Observer because am trying to set discount by code
    – Ramesh S
    Dec 8, 2017 at 6:33
  • Use the dependency injection way to access the function from your Observer.php file, inside your controller
    – MGento
    Dec 8, 2017 at 6:34
  • @MGento: Like this Mage::getModel('epayment/observer')->getAlert();
    – Ramesh S
    Dec 8, 2017 at 6:36
  • @MGento: Am using magento 1.9.3
    – Ramesh S
    Dec 8, 2017 at 6:38

1 Answer 1

3

To dispatch event you have to write:

Mage::dispatchEvent('YOUR_EVENT_NAME', [YOUR_DATA]);

For example:

Mage::dispatchEvent('my_event', ['order' => $order]);

You should put this code inside of if in controller. Then you must configure event listener in etc/config.xml:

<events>
  <my_event>
    <observers>
      <some_fancy_observer_name>
        <type>singleton</type>
        <class>EPayment_EPayment_Model_Observer</class>
        <method> setDiscount </method>
      </some_fancy_observer_name>
     </observers>
  </some_fancy_observer_name>
</events>

And then put required logic inside of your observer.

4
  • Bro thanks its working i mean discount price apply. But here am creating button if i click button apply discount else discount don't apply
    – Ramesh S
    Dec 8, 2017 at 7:30
  • Do you mind to elaborate or preferably ask new question? Dec 8, 2017 at 7:33
  • Sure bro i will ask new one. will accept your answer here
    – Ramesh S
    Dec 8, 2017 at 7:34
  • bro see this magento.stackexchange.com/questions/205026/…
    – Ramesh S
    Dec 8, 2017 at 7:48

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.