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

青春壹個敷衍的年華 提交于 2019-11-28 07:50:22

Was having a hard time with this, but found that this got me over the hump:

                $items[] = array(
                'title' => 'test title',
                'option_id' => '',
                'delete' => '',
                'type' => 'radio',
                'required' => 1,
                'position' => 0);

            $selections = array();
            $selectionRawData[] = array(
                'selection_id' => '',
                'option_id' => '',
                'product_id' => '159',
                'delete' => '',
                'selection_price_value' => '10',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0);
            $selections[] = $selectionRawData;

            $productId = 182;
            $product    = Mage::getModel('catalog/product')
            ->setStoreId(0);
            if ($productId) {
                $product->load($productId);
            }
            Mage::register('product', $product);
            Mage::register('current_product', $product);
            $product->setCanSaveConfigurableAttributes(false);
            $product->setCanSaveCustomOptions(true);

            $product->setBundleOptionsData($items);
            $product->setBundleSelectionsData($selections);
            $product->setCanSaveCustomOptions(true);
            $product->setCanSaveBundleSelections(true);

            $product->save();

Specifically, the

                Mage::register('product', $product);
            Mage::register('current_product', $product);

was the key

EDIT:: Also looks like there's a bit of a peculiarity when trying to add multiple options / selections. The setBundleOptionsData takes an array of options i.e.

Array(
[1] => Array
    (
        [title] => Option 2
        [option_id] => 
        [delete] => 
        [type] => select
        [required] => 1
        [position] => 
    )

[0] => Array
    (
        [title] => Option 1
        [option_id] => 
        [delete] => 
        [type] => select
        [required] => 1
        [position] => 
    ))

And then the selections will be an array of selections arrays with their indexes corresponding to the options array:

Array(
[1] => Array
    (
        [2] => Array
            (
                [selection_id] => 
                [option_id] => 
                [product_id] => 133
                [delete] => 
                [selection_price_value] => 0.00
                [selection_price_type] => 0
                [selection_qty] => 1
                [selection_can_change_qty] => 1
                [position] => 0
            )

        [3] => Array
            (
                [selection_id] => 
                [option_id] => 
                [product_id] => 132
                [delete] => 
                [selection_price_value] => 0.00
                [selection_price_type] => 0
                [selection_qty] => 1
                [selection_can_change_qty] => 1
                [position] => 0
            )

    )

[0] => Array
    (
        [0] => Array
            (
                [selection_id] => 
                [option_id] => 
                [product_id] => 206
                [delete] => 
                [selection_price_value] => 0.00
                [selection_price_type] => 0
                [selection_qty] => 1
                [selection_can_change_qty] => 1
                [position] => 0
            )

        [1] => Array
            (
                [selection_id] => 
                [option_id] => 
                [product_id] => 159
                [delete] => 
                [selection_price_value] => 0.00
                [selection_price_type] => 0
                [selection_qty] => 1
                [selection_can_change_qty] => 1
                [position] => 0
            )

    ))
         $MyOptions[0] = array (
            'title' => 'My Bad','default_title' => 'My Bad',
            'delete' => '',
            'type' => 'radio',
            'required' => 0,
            'position' => 0
        );

or

$optionModel = Mage::getModel('bundle/option') ->addSelection('op111') ->setTitle('op111') ->setDefaultTitle('op111') ->setParentId($product_id) ->setStoreId($product->getStoreId()); $optionModel->save();

Knowledge Craving

I am not using any Web Services, for this. I have simply used the following methods meant specifically for the Bundled Products, which are:-

  1. setBundleOptionsData()
  2. setBundleSelectionsData()
  3. setCanSaveBundleSelections(true)

For the first method, the details of the Bundle Options are provided to the method as the parameter in the form of an array. Similarly, for the second method "setBundleSelectionsData()", we are providing the details of the Bundle Selections to this method as the parameter in the form of an array.

This is the main logic in what goes through for adding any Bundled Product in Magento. Hope this helps to any newbies!!!


Please check this link for more details on Bundle Product creation in a proper way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!