get custom attributes from an Mage_Sales_Model_Order_Item object
I’m writing an Observer for manage the order’s items, I need to send an email for every order based on some custom attributes.
The item object is Mage_Sales_Model_Order_Item and searching around I’ve tried methods like getData(’my_code’), getCustomAttribute, getAttributeText without success.
I need to get the category, size, color and some custom attributes… Here my little code
class Example_OrderMod_Model_Observer{
public function doSomething($observer){
$order = $observer->getEvent()->getOrder();
$id_ordine =开发者_JS百科 $order->getRealOrderId();
$cliente = $observer->getEvent()->getOrder()->getCustomerName();
foreach ($order->getAllItems() as $item) {
//$item is an instance of Mage_Sales_Model_Order_Item
$quantita = $item->getQtyOrdered();
$codice_giglio = $item->getSku();
//echo $item->getData('size');
var_dump($item->getAttributeText('size'));
var_dump($item->getProductOptionByCode('size'));
var_dump($item->getProductOptionByCode('famiglia'));
}
// die();
}
}
any ideas?
many thanks
You'll probably want to load up the product object, and then get your data off of that object. That will allow you to utilize all the methods you are looking for:
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$size = $product->getAttributeText('size');
If $item is instance of Mage_Sales_Model_Order_Item you could simply use:
$product = $item->getProduct();
solution of display custom attributes
function getAttr($product_id, $attributeName) {
$product = Mage::getModel('catalog/product')->load($product_id);
$attributes = $product->getAttributes();
$attributeValue = null;
if(array_key_exists($attributeName , $attributes)) {
$attributesobj = $attributes["{$attributeName}"];
$attributeValue = $attributesobj->getFrontend()->getValue($product);
}
return $attributeValue;
}
When i tried the above methods it didn't worked somehow in my observer class. Actually the product model is unable to load with the above code block. I found this code and it worked. It loads the product model in observer.
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku($item->getSku());
$product->load($productId);
精彩评论