Jen
Jen

Reputation: 111

Custom observer not being triggered

I am trying to write an observer for magento that will be triggered when an order has been marked as shipped and has been given a tracking number.

When I go in through the admin and place an order, invoice and then go to ship the function I need to call is never actually called, and I do not understand why.

I have go through a couple of of pages on the magento website to see what I might be doing wrong, but I just cant figure it out (http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method & http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-2-the-magento-config).

Please note that I do see the module within the admin Advanced area and it is enabled.

If anyone could look over the code I have attached and let me know where my issue is, it would be much appreciated.

Here is my Observer class which is located in app/code/local/WR/TrackingEmail/Model/Observer.php

class WR_TrackingEmail_Model_Observer
{
    public function sendTrackEmail($observer)
    {
        $track = $observer->getEvent()->getTrack();
        $shipment = $track->getShipment(true);
        $shipment->sendEmail();
    }
}

Here is my config.xml for the module

<config>
    <global>
        <modules>
            <wr_trackingemail>
                <version>0.1.1</version>
            </wr_trackingemail>
        </modules>
        <events>
            <sales_order_shipment_track_save_after>
                <observers>
                    <Wr_trackingemail_model_observer>
                        <type>singleton</type>
                        <class>WR_TrackingEmail_Model_Observer</class>
                        <method>sendTrackEmail</method>
                    </Wr_trackingemail_model_observer>
                </observers>
            </sales_order_shipment_track_save_after>
        </events>
    </global>
</config>

Here is my app/etc/modules/WR_TrackingEmail.xml

<config>
    <modules>
        <WR_TrackingEmail>
            <active>true</active>
            <codePool>local</codePool>
        </WR_TrackingEmail>
    </modules>
</config>

Upvotes: 3

Views: 3926

Answers (2)

Alana Storm
Alana Storm

Reputation: 166166

Your statement "Custom observer not being triggered" leaves a lot of room for interpretation. Here's what you'll want to check.

Your observer appears to be setup correctly (although the modules tag belongs outside the global tag, but that doesn't appear to matter for this case). You can test your setup by running the following code yourself from a blank controller action (or other bootstrapped, event loaded, Magento script)

Mage::dispatchEvent('sales_order_shipment_track_save_after');

and then replacing your sendTrackEmail with this

public function sendTrackEmail($observer)
{
    exit(__METHOD__);
}

If execution halts with the text

WR_TrackingEmail_Model_Observer::sendTrackEmail

you'll know your event is configured correctly.

If your event IS configured correctly, the next step is to ensure that the event is actually being fired when you perform the steps above. You can log these events in app/Mage.php by adding this temporary logging code

public static function dispatchEvent($name, array $data = array())
{
    //brain dead logging
    file_put_contents('/tmp/events.log',"$name\n",FILE_APPEND);
    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    #$result = self::registry('events')->dispatch($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

Also, there's a good chance that leaving your exit in above will still result in execution halting if your event is being fired.

If you've determined your observer is configured correctly, AND your event is being fired, then the problem isn't an event being triggered, but your observer code not doing what you think its doing. Re-add your code but keep your exit in place

class WR_TrackingEmail_Model_Observer
{
    public function sendTrackEmail($observer)
    {
        $track = $observer->getEvent()->getTrack();
        $shipment = $track->getShipment(true);
        $shipment->sendEmail();
        exit(__METHOD__);
    }
}

This will allow you to reload the browser over and over again to test your observer code. Good luck!

Upvotes: 11

benmarks
benmarks

Reputation: 23205

Have you registered your module under app/etc/modules/ in a .xml file?

<?xml version="1.0"?>
<config>
    <modules>
        <Wr_Trackingemail>
            <active>true</active>
            <codePool>local</codePool>
        </Wr_Trackingemail>
    </modules>
</config>

Upvotes: 1

Related Questions