Magento adding a bundled product to cart with 'bundle_option'?

Magento adding a bundled product to cart with 'bundle_option'?

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento adding a bundled product to cart with 'bundle_option'?

Addressing Magento Bundled Product Addition to Cart

In Magento, adding a bundled product to the cart programmatically requires careful handling of the bundle_option field in the options array. Let’s dive into your question and provide a step-by-step guide using the latest practices.

Understanding Magento’s Bundle Option Array

The bundle_option array is a critical part of adding a bundled product to the cart. It specifies which optional products are included with the main bundle item. The structure looks like this:

'bundle_option' => [
    0 => [
        'product_id' => <optional_product_id_1>,
        'qty' => <quantity_of_optional_product_1>
    ],
    1 => [
        'product_id' => <optional_product_id_2>,
        'qty' => <quantity_of_optional_product_2>
    ],
    ...
]

Correctly Building the Bundle Option Array

In your code, you’re on the right track by compiling the bundle_option and bundle_option_qty arrays. However, let’s refactor this to follow best practices:

$bundleOptions = [];
$bundleOptionQtys = [];

foreach ($json_obj['basket']['products'] as $product) {
    $productId = (int)$product['id'];
    if (!isset($bundleOptionQtys[$productId])) {
        $bundleOptionQtys[$productId] = 0;
    }
    $bundleOptionQtys[$productId]++;
}

foreach ($bundleOptionQtys as $productId => $quantity) {
    $bundleOptions[] = [
        'product_id' => $productId,
        'qty' => $quantity
    ];
}

Adding the Bundle Product to the Cart

Now, let’s add the bundle product with the correctly built options array:

$bundleProduct = Mage::getModel('catalog/product')->load(833);
$cart = Mage::getSingleton('checkout/cart');

// Add individual products to cart (optional)
foreach ($json_obj['basket']['products'] as $product) {
    $params = [
        'product' => new Varien_Object(['sku' => $product['id'], 'qty' => 1])
    ];
    $cart->addProduct($bundleProduct, $params);
}

// Add bundle product to cart
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

$message = $this->__('Notice: %s item(s) were successfully added to your shopping cart.', count($json_obj['basket']['products']));
Mage::getSingleton('checkout/session')->addSuccessMessage($message);

Important Considerations

  1. Use Varien_Object for Product Loading: In the individual product addition loop, using Varien_Object is more efficient than creating a new instance of Mage_Catalog_Model_Product.

  2. Error Handling: Ensure you handle any potential exceptions that may occur during the cart addition process.

  3. Performance Optimization: If you’re dealing with a large number of products, consider batch processing to minimize memory load.

Conclusion

Adding bundled products to the Magento cart programmatically requires careful management of the bundle_option array. By following these steps and the provided best practices, you can successfully add bundled items to your customer’s shopping carts without any issues.

Remember, understanding the inner workings of Magento is key to maximizing its potential for e-commerce success. For more in-depth guides on Magento development, consider exploring our comprehensive resources at LaravelCompany.

Checkout Catalog JavaScript

Our network