views:

204

answers:

2

I have added a Bundled Product in Magento programmatically, using all the options / properties of the Simple Product & the use of the 3 methods specifically for Bundle Options:-

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

The result is that the newly added Bundle Product shows just fine in the Admin section, with some default values taken of the following properties:-

  1. sku_type
  2. weight_type
  3. shipment_type
  4. price_view
  5. price_type

However, this Bundle Product is not showing in the front-end at all, I don't know why. Can please somebody help me out? I have tried searching but to no avail. Please anybody please suggest as to what I'm doing wrong?

Also I need to control the above-mentioned special properties of Bundle Product, so that they can be modified also during programmatically insertion. Can anybody please help me in this point also?

Any help is greatly appreciated.

A: 

After quite a lot of testings, I have found some way inside it.
If you look very clearly the "saveAction()" method, in the "ProductController.php" page of the "Adminhtml" module, you will find there is a line like "$product->save();".

If you make some inroads into this "save()" method, you will see that Magento is using some "setData()" method, for the simple product's properties.

So if you are using the "price", "weight", "sku", "name", "description", "short_description", ... fields as an 1D array, you just need to add the required properties of the Bundle Product to this array list, and Magento will do the rest.

So what I mean is that the array will contain the following elements as simple as that:-

Array
(
  ['name'] => 'simple',
  ['description'] => 'test',
  ['short_description'] => 'simple test',
  ['sku_type'] => 0,
  ['sku'] => 'simple01',
  ['weight_type'] => 1,
  ['weight'] => 12,
  ['price_type'] => 1,
  ['price'] => 250,
  ['shipment_type'] => 0
)

Now you need to pass this array through the "setData()" method, and bingo you go.

Just remember that Magento is really beautiful if you believe, otherwise it is as harsh & rude.
Hope it helps!

Knowledge Craving
A: 

Hi,

Can you please show me your bundle adding code? I am working on this but can't add bundle through code. You help is highly appreciated.

Thanks,

This is what I am doing. Please correct if there is some fault in it. $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;
        }

I am getting this issue:

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
Knowledge Craving
From which folder?
Irfan Ahmed