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 =
Mage::getModel('bundle/option')
->addSelection('op111')
->setTitle('op111')
->setDefaultTitle('op111')
->setParentId($bundle -> getId())
->setStoreId(0)->save();

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s