Magento – How To Add Custom Attribute To Transaction Email

magento-1.7

I have added an custom attribute to products. i want to show this attribute in order transaction email.

Is there a way to do this?

Thanks.

Best Answer

An SO answer provides some great insight on this:

https://stackoverflow.com/a/9259075/1442685

As the answer reveals, email items renderer will be working with order items, not the actual products. So if you've added this attribute to the product, then you can either:

  • Copy the product attribute to the item model during rendering, OR
  • Install this attribute on the item entity, and setup a fieldset copy entry

Obviously the former is simpler, but will create some overhead processing that could impact performance. Once you choose your path there, you can proceed to the item renderer templates:

app/design/frontend/base/default/template/:

  • bundle/sales/order/items/renderer.phtml
  • sales/order/items/renderer/default.phtml
  • downloadable/sales/order/items/renderer/downloadable.phtml

There may be more, depending on what you have done to customize your installation. Also, the above only apply to front-end order item rendering. In any case, extend one or more of these templates to stuff in your attribute, eg:

# File: app/design/frontend/[package]/[theme]/template/sales/order/items/renderer/default.phtml
...
<?php $_item = $this->getItem() ?>
<tr class="border" id="order-item-row-<?php echo $_item->getId() ?>">
    <td><h3 class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></h3>
        <p><?php echo $this->__('Your Custom Attribute: %s', $_item->getYourCustomAttribute()); ?></p>
        <?php if($_options = $this->getItemOptions()): ?>
        <dl class="item-options">
...

Remembering that if your attribute is not available on the item model, you will have to load it from the product instead.