How to assign bundle items to the bundle product programmatically

Today I am going to explain about the Magento Bundle Option, selection and Items with Bundle Product. Some of the guys know about the all types for products in magento. But I will list it out.

1. Simple
2. Virtual
3. Configurable
4. Grouped
5. Bundle
6. Downloadable
Bundle Product can allow simple and virtual product types only. I will show you how the code looks.
Create bundle option programmatically
$bundle – Object instance of Bundle Product

$optionModel =
->setParentId($bundle -> getId())

Store ID should be current store id.

Create bundle selection programmatically.

$values = array(
'option_id' => $ optionModel -> getOptionId(),
'parent_product_id' => $bundle -> getId(),
'product_id' => $curProduct -> getId(),
'position' => 0,
'is_default' => 0,
'selection_price_type' => 0,
'selection_price_value' => 0.0000,
'selection_qty' => 1.0000,
'selection_can_change_qty' => 1

$curProduct – Object instance of simple product or virtual product.

Mage::getModel('bundle/selection') -> setStoreId($curProduct -> getStoreId()) -> setData($values) -> save();

You can use this code with the Model and Controllers. Especially for the Magento Observers Methods.

I hope it helps some of the Magento Developers.

Comments Welcome.

Thanks Guys. Magento Rocks.