Roy Vincent Niepes

Blog and Portfolio

Posted on by

How to display a custom product attributes in magento’s transactional email.

How to display a custom product attributes in magento’s transactional email.

Probably the reason why you’ve landed to this article is because one of your client asked you to display some of the custom product attributes to transactional email of magento. I’ve spent 2 days just to find out how I can do this in magento and luckily I found a solution searching thoroughly the google.

Here’s the files you need to modify.


Find this code in that file

<?php $_item = $this->getItem() ?>
<?php $_order = $this->getItem()->getOrder() ?>

Then add this code below that code

$productId = $_item->getProduct()->getId(); 
$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getAttributes(); // <--- load all the attribute of the product
$attribute_list = array('yourcustomattribute'); // <--- this is the product attirbute you want to display

Then here's the code that will display the custom attributes. You can place this anywhere you want it to display.
In my end, I place it below the SKU.

foreach ($attributes as $attribute) {
    $attributeCode = $attribute->getAttributeCode();
    if(!in_array($attributeCode, $dispAttribs)) continue;
    $label = $attribute->getFrontend()->getLabel($product);
    $value = $attribute->getFrontend()->getValue($product); 
    echo "<p><strong>".  $this->escapeHtml( $label ) .":</strong> " . $this->escapeHtml($value) . "</p>";

Last time I used this code because the approach above kinda OP or unnecessary, the problem is, it's not displaying in the actual email but working in a testmail(I posted a testmail this below ^_^)

<p><strong>Attribute Label: </strong> <?php echo $this->escapeHtml($_item->getProduct()->getData('customattribute')); ?></p>

Now for you to test it, you have to order in your magento site or for a less hassle.

Create mailtest.php in directory where you install your magento
Assuming under public_html/mailtest.php

and then paste this code.


require_once 'app/Mage.php';

// loads the proper email template
$emailTemplate  = Mage::getModel('core/email_template')->loadDefault('sales_email_order_template');

// All variables your error log tells you that are missing can be placed like this:
$emailTemplateVars = array();
$emailTemplateVars['usermessage'] = "blub";
$emailTemplateVars['store'] = Mage::app()->getStore();
$emailTemplateVars['sendername'] = 'sender name';
$emailTemplateVars['receivername'] = 'receiver name';

// order you want to load by ID
$id = isset($_GET['id']) ? $_GET['id'] : 1; // this is for you to select order dynamically by just adding id parameter
$emailTemplateVars['order'] = Mage::getModel('sales/order')->load($id);

// load payment details:
// usually rendered by this template:
// web/app/design/frontend/base/default/template/payment/info/default.phtml
$order = $emailTemplateVars['order'];
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true);

$emailTemplateVars['payment_html'] = $paymentBlock->toHtml();

//displays the rendered email template
echo $emailTemplate->getProcessedTemplate($emailTemplateVars);

Now view the php file you've just created

That's it! Happy Coding!

comments powered by Disqus