Get custom attributes from the Mage_Sales_Model_Order_Item object

Im writing an Observer for managing order items, I need to send an email for each order based on some custom attributes.

The object object is Mage_Sales_Model_Order_Item and Ive searched for proven methods such as getData (my_code), getCustomAttribute, getAttributeText without success.

I need to get the category, size, color and some custom attributes ... Here is my little code

class Example_OrderMod_Model_Observer{

  public function doSomething($observer){
    $order = $observer->getEvent()->getOrder();

    $id_ordine = $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

+5
source share
4 answers

You might want to download a product object and then delete data from this object. This will allow you to use all the methods you are looking for:

$product = Mage::getModel('catalog/product')->load($item->getProductId());
$size = $product->getAttributeText('size');
+6

$item Mage_Sales_Model_Order_Item, :

$product = $item->getProduct();
+4

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;
}
+3
source

When I tried the above methods, it did not work somehow in my observer class. In fact, the product model cannot boot with the above code block. I found this code and it worked. He uploads the product model to the observer.

$product = Mage::getModel('catalog/product');

$productId = $product->getIdBySku($item->getSku());

$product->load($productId);

+2
source

All Articles