Magento Observer Events – Order of Operations Explained

event-observerinventorymagento-1

I am attempting to inject functionality into the catalog_model_product_duplicate event. Part of this module will be to ensure that the stock status of the duplicated product is also duplicated; currently it is not.

I see that CatalogInventory observes this event and sets up some standard stock information. Can I be guaranteed that core events are resolved before my locals? Is there any order of operations here that I can rely upon?

Best Answer

The order that events are dispatched in is dependent on the order in which the modules are loaded. Since you are needing to be sure that the CatalogInventory module's observers fire before yours does, what you need to do is simply configure your module to depend on the Mage_CatalogInventory module. You can do this by adding a depends node to the code in your app/etc/modules/My_Module.xml file:

<config>
    <modules>
        <My_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_CatalogInventory />
            </depends>
        </My_Module>
    </modules>
</config>

The depends node in the above XML is the crucial piece of configuration here, as it forces the Magento core module to load before yours does.

Related Topic