Magento E-commerce Platform Q/ A

How to Automatically Convert Simple Products to Bundle in Magento

The feature might be useful for the stores dealing with large and often updated inventory. In order not to convert each product manually after each inventory update to existing bundle products it is possible to make the process automated.

First we will create the observer that will be performing the function each time a new product is saved.

Announce the observer in config.xml file:



Now we will create class and method indicated in the observer class Ikantam_Diamonds_Model_Observer:

public function addToBundle( Varien_Event_Observer $observer )	{
		$product = $observer->getEvent()->getProduct();
		/*Get any of the bundle product (in the example we took first one) from the data base*/
		$collectionBundle = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToFilter('type_id', array('eq' => 'bundle'));
		$bundle = $collectionBundle->getFirstItem();
		$optionId = 3; //id of the bundle product option.
		/*define if this product exist already in the bundle product */	
		$selectionModel = Mage::getModel('bundle/selection')-getCollection()
			->where('selection.product_id = ?', $productId);

		if (!count($selectionModel)) {
			/*add product to a bundle*/
			$selection = new Mage_Bundle_Model_Selection();
			$item = array(
				"option_id"=> $optionId,
				"parent_product_id" => $bundle->getId(),
				"product_id" => $product->getId(),
				"position" => '0',
				"is_default " => '0',
				"selection_price_type" => '0',
				"selection_price_value" => '0',
				"selection_qty" =>'1',
				"selection_can_change_qty" => '1'


Now all new products with option 3 will be automatically associated with (added to) the first bundle from our data base.  That's it.