Magento: Get Bundled Items By Bundle Product

5 Comments 10th DEC 2009 | Posted by Tomas Novoselic in Magento

Magento: Get Bundled Items By Bundle Product

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 ^_^

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 5 comments (Add Yours +)

  • clockworkgeek Says

    Hello,

    What is the purpose of $option_collection? Is it important to call getOptionsCollection()? The result is assigned and never used.

  • 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

  • Mage guys should create a getBundledProducts() for a product that isBundled(). It would be easier for everyone.

  • 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();
    }

  • great post as usual!

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009