magento - Creation of bundle products in a loop -


i have loop create several bundle products programmatically. creation of new bundles works fine, if have existing bundle , try add/remove options, fails.

if run import code first time, bundles created. second time, options removed , last bundle of array has options set.

$ids = [8663,8665,8664]; foreach ($ids $id) {     createbundle($id); }  function createbundle($id) {     mage::unregister('product');      try {         $bundleproduct = mage::getmodel('catalog/product')->load($id);         mage::register('product', $bundleproduct);          // try assigned options , remove them         $selectioncollection = $bundleproduct->gettypeinstance(true)->getselectionscollection(             $bundleproduct->gettypeinstance(true)->getoptionsids($bundleproduct), $bundleproduct         );          // remove assigned options         // works fine         foreach ($selectioncollection $option)          {             $optionmodel = mage::getmodel('bundle/option');             $optionmodel->setid($option->option_id);             $optionmodel->delete();         }          $bundleoptions = array(             '0' => array( //option id (0, 1, 2, etc)                 'title' => 'item01', //option title                 'option_id' => '',                 'delete' => '',                 'type' => 'select', //option type                 'required' => '1', //is option required                 'position' => '0' //option position             ),             '1' => array(                 'title' => 'item02',                 'option_id' => '',                 'delete' => '',                 'type' => 'multi',                 'required' => '1',                 'position' => '0'             )         );          $bundleselections = array();         $bundleselections = array(             '0' => array( //option id                 '0' => array( //selection id of option (first product under option (option id) have id of 0, second id of 1, etc)                     'product_id' => 8631, //id of product in selection                     'delete' => '',                     'selection_price_value' => '0',                     'selection_price_type' => 0,                     'selection_qty' => 10,                     'selection_can_change_qty' => 0,                     'position' => 0,                     'is_default' => 1                 )             ),             '1' => array( //option id                 '0' => array(                     'product_id' => 8630,                     'delete' => '',                     'selection_price_value' => '0',                     'selection_price_type' => 0,                     'selection_qty' => 1,                     'selection_can_change_qty' => 0,                     'position' => 0,                     'is_default' => 1                 )             )         );          // flags saving custom options/selections         $bundleproduct->setcansavecustomoptions(true);         $bundleproduct->setcansavebundleselections(true);         $bundleproduct->setaffectbundleproductselections(true);          // setting bundle options , selection data         $bundleproduct->setbundleoptionsdata($bundleoptions);         $bundleproduct->setbundleselectionsdata($bundleselections);          $bundleproduct->save();         $bundleproduct->setdata(array());         return $bundleproduct->getid();      } catch (exception $e) {         mage::log($e->getmessage());         echo $e->getmessage();     } } 

so, how can work? if remove part options deleted, every time code executed there new empty options created (which wrong).

update: found out, bundles created/updated correctly, if run script thrice, each product id. problem must execution in loop in single request.

finally, found out myself. part of posted code:

    // try assigned options , remove them     $selectioncollection = $bundleproduct->gettypeinstance(true)->getselectionscollection(         $bundleproduct->gettypeinstance(true)->getoptionsids($bundleproduct), $bundleproduct     );      // remove assigned options     // works fine     foreach ($selectioncollection $option)      {         $optionmodel = mage::getmodel('bundle/option');         $optionmodel->setid($option->option_id);         $optionmodel->delete();     } 

seems remove selection part of options. remove full options, had this:

    // try assigned options , remove them     $optioncollection = $_product->gettypeinstance(true)->getoptionscollection($_product);      // remove assigned options     foreach ($optioncollection $option)      {         $option->delete();     } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -