Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get custom attributes from an Mage_Sales_Model_Order_Item object

Tags:

magento

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

like image 333
15 revs, 2 users 97% Avatar asked Jun 07 '11 10:06

15 revs, 2 users 97%


2 Answers

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');
like image 185
Prattski Avatar answered Oct 31 '22 12:10

Prattski


If $item is instance of Mage_Sales_Model_Order_Item you could simply use:

$product = $item->getProduct();
like image 35
Armin Avatar answered Oct 31 '22 11:10

Armin