Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items

Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items

Programmatically Adding Bundle Products in Magento: A Comprehensive Guide

As a seasoned e-commerce developer, I frequently encounter complex tasks such as programmatically adding bundle products in Magento using the SKU or ID of simple items. This process involves understanding the inner workings of Magento’s observer classes and mastering the use of bundle_options and bundle_selections.

Understanding the Magento Bundle Product Creation Process

Magento handles bundled product creation through its Observer class, which utilizes two key methods: setBundleOptionsData() and setBundleSelectionsData(). These magic methods are responsible for configuring the options and selections for your bundle items.

However, finding the exact definition of these methods can be challenging. They seem to rely on PHP magic methods, but the main logic behind them remains obscured without further exploration.

A Step-by-Step Approach to Programmatic Bundle Product Creation

To create a bundled product using simple item SKUs or IDs in Magento programmatically, follow this structured approach:

1. Retrieve Simple Item Data

First, use Magento’s API or repository to fetch the required data for your simple items. You can retrieve the SKU and ID using the following code snippet:

$simpleProduct = $this->productRepository->get($simpleProductId);

2. Prepare Bundle Options and Selections

Prepare an array containing the bundle_options and bundle_selections data based on your requirements.

For example, suppose you have a simple item with the SKU “SIMPLE-SKU-1” and want to create a bundle product with this item as one of its options. The respective array would look like this:

$bundleOptions = [
    'option_id' => 0,
    'title' => 'Item Title',
    'type' => 'select',
    'is_require' => false,
    'sort_order' => 0,
];

$bundleSelections = [
    [
        'selection_id' => null,
        'position' => 1,
        'option_id' => $optionId, // The ID of the bundle option created above
        'product_id' => $simpleProduct->getId(),
        'quantity' => 2,
        'price' => 0.00,
    ],
];

3. Create and Populate the Bundle Product

Now, create a new instance of the Magento\Bundle\Model\Bundle class and populate it with the prepared options and selections.

$bundle = Mage::getModel('bundle/bundle');

$bundle->setData([
    'simple_product_id' => $simpleProductId,
    'sku' => $bundleSku, // The desired SKU for your bundle product
    'name' => $bundleName, // The desired name for your bundle product
    'price' => 0.00,
]);

// Set the prepared options and selections
$bundle->setBundleOptions($bundleOptions);
$bundle->setBundleSelections($bundleSelections);

// Calculate additional attributes like price if needed
$bundle->getPriceModel()->setItemPriceType($item_price_type)->calculatePrices();

// Save the bundle product
try {
    $bundle->save();
} catch (Exception $e) {
    Mage::logException($e);
}

Best Practices and Additional Considerations

When programmatically creating bundle products in Magento, consider the following best practices:

  1. Always validate the input data to ensure proper functioning and avoid potential security vulnerabilities.
  2. Use appropriate error handling mechanisms to capture exceptions during the creation process.
  3. Perform thorough testing before deploying your solution to a live environment.

Conclusion: Mastering Programmatic Bundle Product Creation

Programmatically adding bundle products in Magento using simple item SKUs or IDs requires a deep understanding of the platform’s inner workings and its observer classes. By following this comprehensive guide, you can successfully create bundled items based on your specific requirements.

Remember to leverage Magento’s extensive documentation and resources available at https://laravelcompany.com to further enhance your development capabilities.

Happy coding!

Catalog API Extensions Configuration

Our network