Magento : Get all sub products of a bundle product in an Order

July 22nd, 2013 by in Development, Magento 0

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)


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.

Written by Nikola Stojiljkovic

Nikola is a Certified Magento Backend Developer with a passion for high quality code that follows standards and best practices. He also leads our Magento development teams so you can expect excellent Magento articles from him.

See all articles from this author »