Thursday, January 19, 2012

Add Items in Order Email template in Magento

Magento Admin panel provides a way to add email template in System->TransactionalEmails.
There are few existing templates like -
New Order Guest Email
New Order User Email
Shipment Email
Invoice,CreditMemo Email
Registration Email  etc

      {{layout handle="sales_email_order_items" order=$order}}   
above line in Order email template gets item information.
app/design/fronend/default/default/layout/sales.xml has following section -
<!--
Email layouts section
-->
    <sales_email_order_items>
            <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
           ------------------------     
          </block>
</sales_email_order_items>

I added a new template information inside above block - 

           <block type="sales/order_email_items" name="newtemplate" template="email/order/newtemplate.phtml"></block>

Now lets open app/design/frontend/default/default/template/email/order/items.phtml.
Just add -
<?php echo $this->getChildHtml('newtemplate') ?>
after following foreach loop.

  <?php $i=0; foreach ($_order->getAllItems() as $_item): ?>
 <?php endforeach; ?>


Now lets create newtemplate.phtml at app/design/frontend/default/default/template/email/order/newtemplate.phtml

this newtemplate basically gets items from another table for which I have created a Model, that model has function getAllItems(), put following code in your newtemplate.phtml -

<?php
$order = $this->getOrder();
$itemUrl =  $this->getUrl();
$itemModel= Mage::getModel('mymodule/mymodel');
$extraItems = $itemModel->getAllItems($order);
$i = 0;
?>
<?php if ($extraItems ): ?>
<?php foreach ($extraItems as $extraItem):?>
<tbody<?php echo $i%2 ? ' bgcolor="#F6F6F6"' : '' ?>>
  <tr>
      <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <?php echo '<a href="'.$itemUrl.'">' ?>
        <?php echo $this->htmlEscape($extraItem->getproduct_name());?>
        </a>
    </td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <?php echo $this->htmlEscape($extraItem->getsku());?>  </td>
    <td align="center" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <?php echo $extraItem->getqty_ordered() * 1;?> </td>
    <td align="right" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> 0  </td>
  </tr>
</tbody>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>

That is it.

Email Order Item template will include these extra items :)







No comments:

Post a Comment