Overriding Send Email Functions with Custom Functions in PHP

email-templatesPHP

I have to pass the transactional email templates lots of custom variables. The solution many have found to this is to load the necessary variables and collections in the email send function, and pass them in the array to the transactional email template. So, for instance, in app/code/core/Mage/Customer/Model/Customer.php we edit the code to look like:

public function sendPasswordResetConfirmationEmail()
{
   $storeId = $this->getStoreId();
    if (!$storeId) {
        $storeId = $this->_getWebsiteStoreId();
    }
    $customVar = $this->getCustomvar();

    $customCollection= Mage::getModel('namespace_extension/customcollection')->load(5);

    $this->_sendEmailTemplate(self::XML_PATH_FORGOT_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
        array('customer' => $this, 'customvar' => $customVar, 'customcollection' => $customCollection), $storeId);

    return $this;
}

With the edit, customvar and customcollection are loaded and passed with the array to the email template. Now one can reference these in the transactional email template by {{var customvar}} or {{var customcollection.name}}, etc.

Obviously, the huge problem with this is that it is editing core files, which should not be done. I am still relatively new to Magento and unsure how I can override or add to these methods without directly editing the core files. I assume it will involve a module of some sort, but I'm really at a loss on how to do this, and time spent searching the web has simply yielded the existing and flawed solution.

Best Answer

In this case you need to rewrite the above specified class using your module. You can use this module. I love to call this module as Rkt_CustomerSPTemplateModifier

File : app\etc\modules\Rkt_CustomerSPTemplateModifier.xml

<config>
    <modules>
        <Rkt_CustomerSPTemplateModifier>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Customer />
            </depends>
        </Rkt_CustomerSPTemplateModifier>
    </modules>
</config>

This file tells to magento that, I am going to include a module Rkt_CustomerSPTemplateModifier in local code pool and it depends on Mage_Customer mdoule.

File: app\code\local\Rkt/CustomerSPTemplateModifier/etc/config.xml

<config>
    <modules>
        <Rkt_CustomerSPTemplateModifier>
            <version>1.0.0</version>
        </Rkt_CustomerSPTemplateModifier>
    </modules>
    <global>
       <models>
          <customer>
              <rewrite>
                  <customer>Rkt_CustomerSPTemplateModifier_Model_Customer</customer>
              </rewrite>
          </customer>
       </models>
    </global>
</config>

This configuration tells to magento that, it should use the custom class Rkt_CustomerSPTemplateModifier_Model_Customer whenever it requires to use Mage_Customer_Model_Customer.

Now we need to define our custom model class Rkt_CustomerSPTemplateModifier_Model_Customer. For this.

File : app\code\local\Rkt/CustomerSPTemplateModifier/Model/Customer.php

<?php

/**
 * Rewriting core class `Mage_Customer_Model_Customer`
 *
 * The methods which are defined inside this class will be used prior to those methods which are
 * defined inside the class `Mage_Customer_Model_Customer`.
 *
 */
class Rkt_CustomerSPTemplateModifier_Model_Customer extends Mage_Customer_Model_Customer
{

     /**
     * Send email with reset password confirmation link
     *
     * @return Mage_Customer_Model_Customer
     */
    public function sendPasswordResetConfirmationEmail()
    {
       $storeId = $this->getStoreId();
        if (!$storeId) {
            $storeId = $this->_getWebsiteStoreId();
        }
        $customVar = $this->getCustomvar();

        $customCollection= Mage::getModel('namespace_extension/customcollection')->load(5);

        $this->_sendEmailTemplate(
            self::XML_PATH_FORGOT_EMAIL_TEMPLATE, self::XML_PATH_FORGOT_EMAIL_IDENTITY,
            array(
                'customer' => $this, 
                'customvar' => $customVar, 
                'customcollection' => $customCollection
            ), 
            $storeId
        );

        return $this;
    }
}

That's it. A tiny module is successfully created. Now clear all caches and then try again.