Thursday, December 5, 2013

Magento: Add additional product/item attributes in order and invoice emails

Magento provides basic information in the default email templates, but each store has their unique requirement to show additional information.
I will show you how to add extra product attribute values, along with order item options and custom options in order emails and invoice emails.
Here is the code that should work for order and invoice emails to get additional PRODUCT ATTRIBUTES displayed:

$productId = $_item->getProduct()->getId(); //for order emails
//$productId = $_item->getProductId(); //for invoice emails
$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getAttributes();

//Get a list of all PRODUCT ATTRIBUTES you want to show in this array...
$dispAttribs = array('hardrive', 'memory', 'processor'); 

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




For displaying CUSTOM OPTIONS and/or ITEM OPTIONS from the item, use this:


foreach($this->getItemOptions() as $opt) {
    if(isset($opt['option_id'])) { //for CUSTOM OPTIONS
            echo "<strong>" . $opt['label'] . ":</strong> ". $opt['option_value'] . "<br />";
    } else { //for ITEM OPTIONS
            echo "<strong>" . $opt['label'] . ":</strong> ". $opt['value'] . "<br />";
    }
}



For adding code to ORDER emails, the file where the code should go is:
app/design/frontend/base/default/template/email/order/items/order/default.phtml
For adding code to INVOICE emails, the file where the code should go is:
app/design/frontend/base/default/template/email/order/items/invoice/default.phtml
Instead of base/default, you can put it in your custom theme location which is obvious.

example:


<?php
$productId = $_item->getProduct()->getId(); //for order emails
$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getAttributes();
$dispAttribs = array('author', 'publishers', 'publication_year', 'language', 'isbn10', 'isbn13');
foreach ($attributes as $attribute) {   
        $attributeCode = $attribute->getAttributeCode();
        if(!in_array($attributeCode, $dispAttribs)) continue;
        $label = $attribute->getFrontend()->getLabel($product);
        $value = $attribute->getFrontend()->getValue($product);
    if($value!='')
    {
        echo "<p><strong>" . $label . ":</strong> " . $value."</p>";
    }
}
?>

Thursday, October 17, 2013

Custom admin theme in Magento


Featured Image
As mentioned on Magento forums the easiest way to achieve this is with overriding adminhtml config with your local custom one and activate it as module.
This is just a small example of different approach with Admin Theme config option in admin panel, to show you how things can be done in different ways in Magento.

Since this is one of those “code talks, talk walks” examples, here it is: admintheme_example.rar.

It’s great example of small Magento module with simple event hooking and adding configuration fields through system.xml.
Follow directory structure, copy files to their place and you will notice new “Admin Theme” option in System->Configuration->General->Design (Default Config scope). Your theme goes in app/design/adminhtml/default/yourthemename folder. It doesn’t need to be whole theme of course, just the files you’re changing.