views:

341

answers:

3

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.

A: 

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!!!

Knowledge Craving
A: 

Any script that can give idea of exact parameter array values to send in both functions?

$product = Mage::getModel('catalog/product'); $product_id = $product->getIdBySku($arg_data['sku']); $product->load($product_id);

    /////////////////////////////////////bundle Product Code///////////////////////////////////
    //================================================
    // this part handles bundle products 
    //================================================

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

             $MySelection[0][0] = array(
                "product_id" => 4,
                "title" => 'dd',
                "position" => 0,
                "selection_price_type" => 0,
                "selection_price_value" => 0.00,
                "selection_qty" => 1.00,
                "selection_can_change_qty" => 1,
                "delete" => ''
            );

            print_r($MyOptions);

            $product->setBundleOptionsData($MyOptions);
            $product->setBundleSelectionsData($MySelection);

            try{
                $product->save();
            }
            catch(Exception $e){
                echo $e;
            }

This is the error i am getting after running this code.

exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null' in /var/www/magento/lib/Zend/Db/Statement/Pdo.php:234 Stack trace:

0 /var/www/magento/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)

1 /var/www/magento/lib/Zend/Db/Adapter/Abstract.php(468): Zend_Db_Statement->execute(Array)

2 /var/www/magento/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)

3 /var/www/magento/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `ca...', Array)

4 /var/www/magento/lib/Zend/Db/Adapter/Abstract.php(546): Varien_Db_Adapter_Pdo_Mysql->query('INSERT INTO `ca...', Array)

5 /var/www/magento/app/code/core/Mage/Bundle/Model/Mysql4/Option.php(75): Zend_Db_Adapter_Abstract->insert('catalog_product...', Array)

6 /var/www/magento/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(422): Mage_Bundle_Model_Mysql4_Option->_afterSave(Object(Mage_Bundle_Model_Option))

7 /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php(306): Mage_Core_Model_Mysql4_Abstract->save(Object(Mage_Bundle_Model_Option))

8 /var/www/magento/app/code/core/Mage/Bundle/Model/Product/Type.php(263): Mage_Core_Model_Abstract->save()

9 /var/www/magento/app/code/core/Mage/Catalog/Model/Product.php(505): Mage_Bundle_Model_Product_Type->save(Object(Mage_Catalog_Model_Product))

10 /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php(307): Mage_Catalog_Model_Product->_afterSave()

11 /var/www/PhpProject1/Product_options.php(54): Mage_Core_Model_Abstract->save()

12 /var/www/PhpProject1/direct_import_bundle_products_options 1.php(83): Product->createBundleProductOptions(Array)

Irfan Ahmed
@Irfan - You will need to view the **ProductController.php**, to understand what Magento is sending for the Bundle Items in the Admin section. It's the best way to understand the Magento structure. If you get into problems, please ask any coding related questions in **Stack Overflow**.
Knowledge Craving
This isn't an answer. If you're having trouble, ask a question.
noah
A: 
         $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();

vnaz