2

I added a custom attribute in to my default product type like so

Attribute Code: mfgpartnumber
Attribute Label: Manufacturer Part Number

This attribute is used to add products in Magento backend and I want to include this attribute in new order emails. I have researched so far and here is what I finally added

<?php $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku(), array('mfgpartnumber')); ?>
<?php echo $product->getAttributeText('mfgpartnumber'); ?> 

To /home/txsystems/store/app/design/frontend/base/default/template/email/order/items/order

That did not work, please can someone point me in the right direction on how to add custom attribute to Magento new order emails.

0

3 Answers 3

5

I prefer not to use loadByAttribute as it returns a resource collection call which uses "select *" by default. Try this instead:

$product = Mage::getModel('catalog/product')->load($_item->getProductId());
0

To add custom fields to Order Email, open the order email template located at

app\locale\en_US\template\email\sales\order_new.html

app\locale\en_US\template\email\sales\order_new_guest.html

add the below code where you want

{{depend order.hasCustomFields()}}

<table cellspacing="0" cellpadding="0" border="0" width="650">

<thead>

<tr>

<th align="left" width="325" bgcolor="#EAEAEA" style="font-size:13px; padding:5px 9px 6px 9px; line-height:1em;">Custom Fields:</th>

</tr>

</thead>

<tbody>

<tr>

<td valign="top" style="font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;">

{{var order.getFieldHtml()}}

&nbsp;

</td>

</tr>

</tbody>

</table>

{{/depend}}

<br/>

So as per the above code, you need two functions hasCustomFields() and getFieldHtml() in our Order class. So next you will override the Mage_Sales_Model_Order model. Open your config.xml file and inside section put in this code

<sales>

<rewrite>

<order>Excellence_Custom_Model_Sales_Order</order>

</rewrite>

</sales>

and next create our new model class Excellence_Custom_Model_Sales_Order

<?php

class Excellence_Custom_Model_Sales_Order extends Mage_Sales_Model_Order{

public function hasCustomFields(){

$var = $this->getSsn();

if($var && !empty($var)){

return true;

}else{

return false;

}

}

public function getFieldHtml(){

$var = $this->getSsn();

$html = '<b>SSN:</b>'.$var.'<br/>';

return $html;
0

A better way is not to load the product by the item ID, but simply getting the product via the item:

$customAttribute = $_item->getProduct()->getCustomAttribute();

or

$customAttributeText = $_item->getProduct()->getAttributeText('custom_attribute');

And using that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.