Magento – Implementing a custom function in Magento

magento-1.7module

I'm trying to implement the 2nd solution from philwinkle in this post:
Price(float number) in transactional email

I have my folder setup like so:
app/code/local/Anja/EmailModule/Model/Email/Template with the file Filter.php

The content of this file is:

 class Anja_EmailModule_Model_Email_Template_Filter extends Mage_Core_Model_Email_Template_Filter
{
    public function __construct()
    {
        parent::__construct();
        $this->_modifiers['formatCurrency'] = array($this, 'modifierFormatCurrency');
    }

    public function modifierFormatCurrency($var)
    {
        return Mage::helper('core')->currency($var,false,false) . " Test";
    }
}

but it doesn't appear to get processed at all by Magento. I have cleared cache and recompiled, but in the output not even the word Test gets displayed. What am I doing wrong? Do I also need an xml file somewhere?

Edit after some googling I find that I just want to extend the class.
I have 2 xml files
app/etc/modules/Anja_EmailModule.xml

<?xml version="1.0"?>

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

and in app/local/Anja/EmailModule/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Anja_EmailModule>
            <version>0.1.0</version>
        </Anja_EmailModule>
    </modules>

    <global>
        <models>
            <emailmodule>
                <rewrite>
                    <email_template_filter>Anja_EmailModule_Model_Email_Template_Filter</email_template_filter>
                </rewrite>
            </emailmodule>
        </models>
    </global>
</config>

from this example: http://www.codexpedia.com/magento/how-to-extend-a-model-class-and-overrides-its-functions-in-magento/

I still don't see the word "Test" in my email.

Best Answer

If you haven't done so, you have to include the rest of the entire custom module. Not just this one file.

You'll need the config.xml, the app/etc/module/modulename.xml, and any support model files and such.

I suggest you look at inchoo or atwix for help creating a magento module. They are online sites that have lots of information about doing things like this.

A quick google search turned up a hello world module to get you going.... Build something simple that works, then modify it to do what you want.

http://inchoo.net/magento/programming-magento/magento-hello-world-module-extension/

EDIT:

In your config.xml add

You have to determine what event you want to observe, put that into the xml file, and then you put the method name in there. You have to determine what event you want to observe, put that into the xml file, and then you put the method name in there.

<events>
    <controller_front_init_routers>
        <observers>
            <Companyname_ModuleName_Model_Observer>
                <type>singleton</type>
                <class>Companyname_ModuleName_Model_Observer</class>
                    <method>yourMethodNameHere</method>
            </Companyname_ModuleName_Model_Observer>
        </observers>
    </controller_front_init_routers>
</events>

then, in /Companyname/ModuleName/Model folder (you create this) add a file called Observer.php

in this, you add

<?php
class Companyname_ModuleName_Model_Observer {
    public function yourMethodNameHere() { echo "DID IT"; };

}

This will fire on every page load. controller_front_init_router runs every time.