Posted by

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

For a brief information, transactional email is an email that is sent to an individual recipient following a commercial transaction or specific action performed by that person. And now you want to add the custom attribute you created in the email that is being sent to all users after the purchase.

Here’s the file you need to update.

app/design/frontend/[replacewithyourtemplate]/default</strong>/template/email/order/items/order/default.phtml

Find this code in that file

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

Then add this code below next to it.

<?php 
$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.

<?php 
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>";
}
?>

You can also try this method instead of going through the code above, however you might encountered some issue but still worthy to experiment. In my end it did not displayed on an actual email but working on the test mail.

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

For convenience, I’ve included a source code for you to be able to test it without going through the actual purchase in magento.

Just create a file mailtest.php in your magento root directory.

<?php 
require_once 'app/Mage.php';
Mage::app();
// 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);
$paymentBlock->getMethod()->setStore(Mage::app()->getStore());  
$emailTemplateVars['payment_html'] = $paymentBlock->toHtml();
//displays the rendered email template
echo $emailTemplate->getProcessedTemplate($emailTemplateVars);

Now view the php file you’ve just created in the browser.

www.yourmagentosite.com/mailtest.php