1

I am trying to list all the email templates on a Magento installation for an extension I am working on. It currently uses the getDefaultTemplatesAsOptionsArray function but that does not list any custom templates that the user has added to the system.

I have been searching around and have not been able to find a reference for a function that would provide this.

2

Create the file in NameSpace\ModuleName\Model\System\Config\Source

<?php
class NameSpace_ModuelName_Model_System_Config_Source_Customtemplate
{
/**
 * Config xpath to email template node
 *
 */
const XML_PATH_TEMPLATE_EMAIL = 'global/template/email/';
/**
 * Generate list of email templates
 *
 * @return array
 */
public function toOptionArray()
{
    $result = array();
    $collection = Mage::getResourceModel('core/email_template_collection')
        ->load();
    $options = $collection->toOptionArray();
    $defOptions = Mage_Core_Model_Email_Template::getDefaultTemplatesAsOptionsArray();
    foreach ($defOptions as $v) {
        $options[] = $v;
    }
    foreach ($options as $v) {
        $result[$v['value']] = $v['label'];
    }
    // sort by names alphabetically
    asort($result);
    if (!$asHash) {
        $options = array();
        $options[] = array('value' => '', 'label' => '---------Choose Email Template---------');
        foreach ($result as $k => $v) {
            if ($k == '')
                continue;
            $options[] = array('value' => $k, 'label' => $v);
        }

        $result = $options;
    }
    return $result;
}
}
?>

And in system.xml file in field tag you have to write below code.

 <customtemplate translate="label">
     <label>Choose Email Template</label>
     <frontend_type>select</frontend_type>    
                                            <source_model>modulename/system_config_source_customtemplate</source_model>
     <sort_order>40</sort_order>
     <show_in_default>1</show_in_default>
     <show_in_website>1</show_in_website>
     <show_in_store>1</show_in_store>
 </customtemplate>  
1
  • This is a pretty old question, but just for reference: there is no need to create a custom source model for email templates. Just use adminhtml/system_config_source_email_template, provided in Magento core. (Also, this answer isn't actually relevant to the original question, which wasn't asking about admin config.) Oct 29 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.