Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

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

I have some simple catalog products in Magento, so I have their SKUs & IDs. Now I want to create a Bundled product using the array elements "bundle_options" & "bundle_selections" of the Bundle Items, which are used by the Magento Admin coding in its Observer Class.

Also in the Observer class, there are method calls of two functions "setBundleOptionsData()" & "setBundleSelectionsData()", for whose I am not able to find any function definition.

Please any professional post here, because I need some proper way of doing this thing. If it needs, overriding modules or using events, I will do, but I need really professional help. Thanks in advance.

Edit:-
Regarding the two methods mentioned above "setBundleOptionsData()" & "setBundleSelectionsData()", what I'm almost certain is that they are using some sort of PHP magic methods, but I don't know where the main logic of these magic methods are written?

Please anybody provide some correct answer. Any help is greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
            )

    ))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...