Magento – How to Create Custom Email Handle in Magento

magento-1.9order-emailupdate-handle

I need to create a new handle (I think) to a module that sends a email order notification to admin. The module uses that same default.phtml template for both the emails sent to admin and to the customer. I need to add custom product attributes to the email that gets sent to the admin but doesn't show up on the emails sent to the customer. If I could use a different default.phtml email template for the email sent to admin seems to be what I need.

I found some tutorials about creating custom handles but nothing about creating email handles. Following this Magento Observers, Layout Handles, & Frontend Page Views Here's what I have so far.

In my modules config.xml I added <controller_action_layout_load_before> like this:

    <config>
    <modules>
        <Inchoo_AdminOrderNotifier>
            <version>1.0.0.0</version>
        </Inchoo_AdminOrderNotifier>
    </modules>
    <global>
        <models>
            <inchoo_adminOrderNotifier>
                <class>Inchoo_AdminOrderNotifier_Model</class>
            </inchoo_adminOrderNotifier>
        </models>
        <helpers>
            <inchoo_adminOrderNotifier>
                <class>Inchoo_AdminOrderNotifier_Helper</class>
            </inchoo_adminOrderNotifier>
        </helpers>
        <blocks>
            <inchoo_adminOrderNotifier>
                <class>Inchoo_AdminOrderNotifier_Block</class>
            </inchoo_adminOrderNotifier>
        </blocks>
        <template>
            <email>
               <sales_inchoo_adminOrderNotifier_email_template>
                   <label>Inchoo Admin Order Notifier</label>
                   <file>inchoo_adminOrderNotifier.html</file>
                   <type>html</type>
               </sales_inchoo_adminOrderNotifier_email_template>
            </email>
        </template>
        <events>
            <sales_order_place_after>
                <observers>
                    <inchoo_adminOrderNotifier_sendNotificationEmailToAdmin>
                        <class>inchoo_adminOrderNotifier/observer</class>
                        <method>sendNotificationEmailToAdmin</method>
                    </inchoo_adminOrderNotifier_sendNotificationEmailToAdmin>
                </observers>
            </sales_order_place_after>
        </events>
    </global>


<frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <inchoo_adminOrderNotifier_sendNotificationEmailToAdmin>
                        <class>inchoo_adminOrderNotifier/observer</class>
                        <method>addHandles</method>
                    </inchoo_adminOrderNotifier_sendNotificationEmailToAdmin>
                </observers>
            </controller_action_layout_load_before>
        </events>
</frontend> 


</config>

In my Observer.php I added the code (at the bottom) …I know is incorrect but I think I have it in the correct location. 🙂

    class Inchoo_AdminOrderNotifier_Model_Observer extends Mage_Core_Helper_Abstract
    {
        public function sendNotificationEmailToAdmin($observer)
        {
            $order = $observer->getEvent()->getOrder();
            $storeId = $order->getStoreId();

            $helper = Mage::helper('inchoo_adminOrderNotifier');

            if (!$helper->isModuleEnabled($storeId)) {
                return;
            }

            try {
    $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
            ->setIsSecureMode(true);
            $paymentBlock->getMethod()->setStore($storeId);
            $paymentBlockHtml = $paymentBlock->toHtml();

            $emailTemplateVariables = array(
                'order'        => $order,
                'billing'      => $order->getBillingAddress(),
                'payment_html' => $paymentBlockHtml
            );          

                $templateId = $helper->getEmailTemplate($storeId);

                $mailer = Mage::getModel('core/email_template_mailer');

                if ($helper->getNotifyGeneralEmail()) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($helper->getStoreEmailAddressSenderOption('general', 'email'), $helper->getStoreEmailAddressSenderOption('general', 'name'));
                    $mailer->addEmailInfo($emailInfo);
                }

                if ($helper->getNotifySalesEmail()) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($helper->getStoreEmailAddressSenderOption('sales', 'email'), $helper->getStoreEmailAddressSenderOption('sales', 'name'));
                    $mailer->addEmailInfo($emailInfo);
                }

                if ($helper->getNotifySupportEmail()) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($helper->getStoreEmailAddressSenderOption('support', 'email'), $helper->getStoreEmailAddressSenderOption('support', 'name'));
                    $mailer->addEmailInfo($emailInfo);
                }

                if ($helper->getNotifyCustom1Email()) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($helper->getStoreEmailAddressSenderOption('custom1', 'email'), $helper->getStoreEmailAddressSenderOption('custom1', 'name'));
                    $mailer->addEmailInfo($emailInfo);
                }

                if ($helper->getNotifyCustom2Email()) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($helper->getStoreEmailAddressSenderOption('custom2', 'email'), $helper->getStoreEmailAddressSenderOption('custom2', 'name'));
                    $mailer->addEmailInfo($emailInfo);
                }

                foreach ($helper->getNotifyEmails() as $entry) {
                    $emailInfo = Mage::getModel('core/email_info');
                    $emailInfo->addTo($entry['email'], $entry['name']);
                    $mailer->addEmailInfo($emailInfo);
                }

                $mailer->setSender(array(
                    'name' => $helper->getStoreEmailAddressSenderOption('general', 'name'),
                    'email' => $helper->getStoreEmailAddressSenderOption('general', 'email'),
                ));

                $mailer->setStoreId($storeId);
                $mailer->setTemplateId($templateId);
                $mailer->setTemplateParams($emailTemplateVariables);

                $mailer->send();
            } catch (Exception $e) {
                Mage::logException($e);
            }
        }

<?php   /** I added this **/ ?>
    public function addHandles($observer) {
            $category = Mage::registry('current_category');
            if ($category instanceof Mage_Catalog_Model_Category) {
                $update = Mage::getSingleton('core/layout')->getUpdate();
                $fertilility = (<a href="http://www.php.net/count" target="_blank">count</a>($category->getChildrenCategories()->getData())) ? 'parent' : 'nochildren';
                $update->addHandle('catalog_category_' . $fertilility);
            }
            return $this;
        }   
    }

What I think I need to do and trying to do is to create a handle like {{layout handle="sales_admin_email_order_items" order=$order}} …the original handle is {{layout handle="sales_email_order_items" order=$order}}

Then in my local.xml (if all works) I should be able to add my new email handle like this (except changing the default.phtml):

    <sales_admin_email_order_items>
    <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_order_default</block><template>email/order/items/order/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>email/order/items/order/default.phtml</template></action>
        <block type="sales/order_totals" name="order_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
            </block>
        </block>
    </block>
    <block type="core/text_list" name="additional.product.info" />
</sales_admin_email_order_items>

correct?

Anyway, the problem is I don't know how to code the Observer.php file correctly and can't find it on Google. Any help with this would be very much appreciated.

Best Answer

After much searching and tinkering I finally got this. I was WAY OFF but it turned out to be easier than I thought. I used the following tutorial Magento: Adding a new block to the e-mail templates

Here's what I done:

In app/locale/en_US/template/email/inchoo_adminOrderNotifier.html I changed

{{layout handle="sales_email_order_items" order=$order}}

to

{{layout handle="sales_admin_email_order_items" order=$order}}

In sales.xml (after moving it to default/default/layout) I add this:

<sales_admin_email_order_items>
            <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_order_default</block><template>email/order/items/order/admin-default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>email/order/items/order/admin-default.phtml</template></action>
        <block type="sales/order_totals" name="order_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
            </block>
        </block>
    </block>
    <block type="core/text_list" name="additional.product.info" />

Then in /template/email/order/items/order/default.phtml I created a new default.phtml and called it admin-default.phtml that has my customizations. That was it.

Related Topic