Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom attribute variable to order confirmation e-mail template

Tags:

magento

I have created several custom attributes for my products. I have some that need to be added to the email that the costumer receives when he places an order. Currently I found that I can edit the mail template in the backend and 'app/design/frontend/base/default/template/email/order/items.phtml'.

How to access object variables in e-mail templates and what is the proper syntax for item custom attributes?

like image 637
Jarco Avatar asked Dec 21 '22 02:12

Jarco


1 Answers

There are several possibilities (this being Magento).

The template app/design/frontend/base/default/template/email/order/items.phtml you mentioned is the wrapper for all the items in the order. The individual items are rendered by separate templates. Most of the time that is app/design/frontend/base/default/template/email/order/items/order/default.phtml, but other product types can use different ones, e.g. order items for bundled products use ...template/bundle/email/order/items/order/default.phtml.

Inside the templates you are not working with Mage_Catalog_Model_Product model instances, but with Mage_Sales_Model_Order_Item instances. These items have different attributes then product attributes.
If you want to have your custom product attributes always to be available on the sales/order_item instances, you will need to add them to the entity.
During the checkout process, also set them on the sales/quote_item entity and them copy them over to the order items using the appropriate fieldsets in the configuration (see global/fieldsetsin Mage/Sales/etc/config.xml for an example.

If the products still exist in the catalog, they can be loaded including all attributes using

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

Or, to fetch all product models for all order items at once (slightly more efficient):

$products = Mage::getResourceModel('catalog/product_collection')
    ->addIdFilter($orderItems->getColumnValues('product_id'))
    ->addAttributeToSelect(array('list', 'of', 'your', 'custom', 'attributes'));

Of course, this will mean additional queries as compared to the first solution of adding the same attributes to the order item entity.
Also be aware that once a product is deleted it won't be loadable in this way any more.

like image 163
Vinai Avatar answered May 12 '23 03:05

Vinai