Magento: Get Bundled Items By Bundle Product
5 Comments 10th DEC 2009 | Posted by Tomas Novoselic in Magento

This post is just a small snippet that will help you to get all products that are used do construct any bundle product.
Of course, I’m not sure how useful can it be as is, so if you need optimized version of it, you will need to play with it for a while.
You will need some basic knowledge of PHP language in order to fix formatting issues that you may (not saying that you will) encounter due strange behavior of code display plugin in wordpress.
I tried to find it on google few days ago and I had no luck, so if anybody has better solution, please comment.
Here we go:
< ?php
$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load(YOUR_BUNDLED_PRODUCT_ID);
$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);
$bundled_items = array();
foreach($selectionCollection as $option)
{
$bundled_items[] = $option->product_id;
}
print_r($bundled_items);
?>
That easy ^_^
To post code in comments, place your code inside [code] and [/code] tags.


















January 12th, 2010 at 19:52
Hello,
What is the purpose of $option_collection? Is it important to call getOptionsCollection()? The result is assigned and never used.
January 13th, 2010 at 8:20
Hi clockworkgeek,
U R right, I have fixed that, tnx for noticing.
Somehow I overlooked it when I was pulling code out of the original context XD
March 1st, 2010 at 15:49
Mage guys should create a getBundledProducts() for a product that isBundled(). It would be easier for everyone.
April 13th, 2010 at 19:57
if($product->hasCustomOptions()){
$customOption = $product->getCustomOption(‘bundle_selection_ids’);
$selectionIds = unserialize($customOption->getValue());
$selections = $this->getSelectionsByIds($selectionIds, $product);
foreach ($selections->getItems() as $selection)
$ids[] = $selection->getId();
}
May 4th, 2010 at 8:41
great post as usual!