0
votes

I currently have a Magento module with an observer that listens for the following events:

  • sales_order_invoice_save_after
  • sales_order_place_after
  • sales_order_shipment_save_after
  • order_cancel_after

The observer calls a method in a Model that packages important details about the order and exports them in the client's choice of output (JSON,CSV,XML) to a remote system.

I have one client that requires some mutation of the data before it's exported, though.

What I'd like to do is extend this module with a custom local module, rather than make a "special" version to maintain forever. I can extend the model easily enough to override the method that would handle this, but I'm not exactly sure how to make sure it gets called.

How can I override the class called by an observer?

Is this even the correct approach for my problem? For instance, would it be more appropriate to have some sort of dispatch Model that looks for a config setting to decide which class will handle the process?

1

1 Answers

0
votes

It's not entirely clear if you're looking to override the observer model itself, or if you're looking to override the model that's called by the observer.

In Mage_Core_Model_App::dispatchEvent(), Magento uses Mage::getModel($obs['model']) to grab the observer model. So, as long as your configuration refers to the observer by its group name (i.e. modulename/modelname instead of Foo_Modulename_Model_Modelname), then you can use standard Magento rewrites to change the observer model in a separate module:

<config>
  <global>
    <models>
      <!-- this is the group name of the module's models -->
      <modulename>
        <rewrite>
          <!-- rewrite the modulename/modelname model to use a different class -->
          <modelname>Bar_Modulename_Model_Modelname</modelname>
        </rewrite>
      </modulename>
    </models>
  </global>
</config>

This works the same way if you're looking to override another model, too. For example, if your observer calls Mage::getModel('mymodule/order_export'), then you should rewrite the mymodule/order_export class the same way.