Imagine the following situation: You have an order (or a collection of orders) that contain bundle products in them. To cover some requested functionality in your custom Magento module, you need to grab sub products of the ordered bundle product. As always with Magento, the code that covers this is fairly simple – if you happen to find the right way to do it So, here it goes.
$collection = Mage::getResourceModel('sales/order_item_collection') ->addFieldToFilter('order_id', array('eq' => $your_order_id)); foreach ($collection as $item) { // if you want to filter out master bundle products for any reason // and grab only its child simple products, you can do it with a simple IF statement: // if ($item->getData('product_type') !== Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) var_dump($item->getData('name')); }
The main trick here is to use Magento’s ‘sales/order_item_collection‘ model to get all of the items in an order. There is an alternative way to do this with ‘sales/order‘ model by using the getItemsCollection() function, but I like to access the info I need directly. It’s usually the fastest that way and you only grab the data you need.