Magento E-commerce Platform Q/ A

How to Add a Custom Footer to Email Templates in Magento

Here's the example of the footer and how to do it:

247

Namespace - Ikantam

Module - EmailFooter

In the example the footer text is extracted from a readymade template. Since templates might be both text and html, the appropriate footer should be applied to each, i.e. for the html templates – html footer template, for the text templates – text footer template. How to create custom email templates you can read here

 

The footer will appear only in emails sent with the help of 'sendTransactional' method of 'Mage_Core_Model_Email_Template' model.

To add custom footer we will rewrite 'Mage_Core_Model_Email_Template' model

<?php

class Ikantam_EmailFooter_Model_Core_Email_Template extends Mage_Core_Model_Email_Template
{
    /**
     * Send transactional email to recipient
     *
     * @param   int $templateId
     * @param   string|array $sender sneder informatio, can be declared as part of config path
     * @param   string $email recipient email
     * @param   string $name recipient name
     * @param   array $vars varianles which can be used in template
     * @param   int|null $storeId
     * @return  Mage_Core_Model_Email_Template
     */
    public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
    {
        $this->setSentSuccess(false);
        if (($storeId === null) && $this->getDesignConfig()->getStore()) {
            $storeId = $this->getDesignConfig()->getStore();
        }

        if (is_numeric($templateId)) {
            $this->load($templateId);
        } else {
            $localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
            $this->loadDefault($templateId, $localeCode);
        }

        /*-------REWRITE-------------------------*/

        if (Mage::getStoreConfig('email_footer/settings/enable')) {
            $this->addFooterTemplate();
        }

        /*---------------------------------------*/

        if (!$this->getId()) {
            throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid transactional email code: ' . $templateId));
        }

        if (!is_array($sender)) {
            $this->setSenderName(Mage::getStoreConfig('trans_email/ident_' . $sender . '/name', $storeId));
            $this->setSenderEmail(Mage::getStoreConfig('trans_email/ident_' . $sender . '/email', $storeId));
        } else {
            $this->setSenderName($sender['name']);
            $this->setSenderEmail($sender['email']);
        }

        if (!isset($vars['store'])) {
            $vars['store'] = Mage::app()->getStore($storeId);
        }
        $this->setSentSuccess($this->send($email, $name, $vars));
        return $this;
    }


    public  function addFooterTemplate()
    {

        $model = new self();
        
        if  ($this->getTemplateType() == self::TYPE_HTML) {
            $footerTemplateId = Mage::getStoreConfig('email_footer/settings/html_email_template');
        } else {
            $footerTemplateId = Mage::getStoreConfig('email_footer/settings/text_email_template');
        }


        if (is_numeric($footerTemplateId)) {
            $model->load($footerTemplateId);
        } else {
            $localeCode = Mage::getStoreConfig('general/locale/code');
            $model->loadDefault($footerTemplateId, $localeCode);
        }
        $templateText = $this->getTemplateText();
        $footerTemplateText = $model->getTemplateText();
        
       
     
   // text template
        if ($this->getTemplateType() == self::TYPE_TEXT) {
            $this->setTemplateText($templateText . $footerTemplateText);
            return $this;
        }

   // html  template    
        require_once Mage::getBaseDir('lib') . DS . 'simple_html_dom' . DS . 'simple_html_dom.php';


        $html = str_get_html($templateText);

        if ($bodyArray = $html->find('body > table > table')) {
            $body = array_shift($bodyArray);
            $body->outertext .= $footerTemplateText;
            $newTemplateText = $html->outertext;
        } else {
            $newTemplateText = $templateText . $footerTemplateText;
        }




        $this->setTemplateText($newTemplateText);
        return $this;
    }


}

In the model we will add method 'addFooterTemplate()' method - this method will add the custom footer in the email template. The variable ‘$footerTemplateId’ stores template ID with the custom footer. In this example the ID is read from the config file.

For the html templates in order to put the custom footer into email html-tags we will use PHP Simple HTML DOM Parser library. With its help we will put custom footer into <table> tag, if the tag does not exist create one.

For the text template, we simply add the footer text to the email template.

That's it.