title: Magento How to update bundle product programmatically
body: <p>I am reading product sku from csv file and my csv file contains bundle product sku. I am traversing through csv data and for each bundle sku I want to add bundle items inside it which I am passing through CSV</p>
  
  <p>Here is the code what I have done</p>
  
  <pre><code>ini_set('auto_detect_line_endings', TRUE);
  $magentoPath = getcwd();
  require_once ($magentoPath . '/includes/config.php');
  require_once ($magentoPath . '/app/Mage.php');
  Mage::app();
  
  //read the csv
  $bundleCsv = Mage::getBaseDir('var').'/import/bundleImport.csv';
  $rows = array_map('str_getcsv', file($bundleCsv));
  $header = array_shift($rows);
  $csv = array();
  
  foreach ($rows as $row) {
      $csv[] = array_combine($header, $row);
  }
  
  Mage::app()-&gt;setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  
  echo Mage::app()-&gt;getStore()-&gt;getId(); exit;
  
  foreach( $csv as $key =&gt; $val ){
      if( !isset($val['sku']) || empty($val['sku']) ||  $val['sku'] == '' ){
          echo 'Not Valid Sku';
          continue;
      }
  
      $_product = Mage::getModel('catalog/product')-&gt;loadByAttribute('sku',$val['sku']);
  
      $opt = $val['bundle_options'];
      $optArr = explode(':', $opt);
  
      $bundleOptions = array();
      $bundleSelections = array();
      foreach ( $optArr as $key1 =&gt; $val1 ) {
          $valTemp = explode( '(', $val1 );
          $title = trim($valTemp[0]);
          $bundleSub[$key1] = array(
                  'title' =&gt; $title, // option title
                  'option_id' =&gt; $key1,
                  'delete' =&gt; '',
                  'type' =&gt; 'select', // option type
                  'required' =&gt; '1', // is option required
                  'position' =&gt; '1'  // option position
          );
  
          $skuStr = trim($valTemp[1]);
          $skuStrTemp = explode( ')', $skuStr );
          $skuStr = trim($skuStrTemp[0]);
  
          $skuTemp = explode( '+', $skuStr );
  
          foreach( $skuTemp as $key2 =&gt; $val2 ){
              $product = Mage::getModel('catalog/product');
              $id = Mage::getModel('catalog/product')-&gt;getResource()-&gt;getIdBySku($val2);
  
              if( $id ){
                  $bundleSelectionsSub[$key2] = array ( // selection ID of the option (first product under this option (option ID) would have ID of 0, second an ID of 1, etc)
                          'product_id' =&gt; $id, // if of a product in selection
                          'delete' =&gt; '',
                          'selection_price_value' =&gt; '10',
                          'selection_price_type' =&gt; 0,
                          'selection_qty' =&gt; 1,
                          'selection_can_change_qty' =&gt; 0,
                          'position' =&gt; 0,
                          'is_default' =&gt; 1
                  );
                  $product = null;
              }else{
                  continue;
              }
          }
          $bundleSelections[$key1] = $bundleSelectionsSub;
      }
  
      $bundleOptions = $bundleSub;
      //echo '&lt;pre&gt;'; print_r($bundleOptions); exit;
      try{
  
          $_product-&gt;setCanSaveCustomOptions ( true );
          $_product-&gt;setCanSaveBundleSelections ( true );
          $_product-&gt;setAffectBundleProductSelections ( true );
  
          $_product-&gt;setBundleOptionsData ( $bundleOptions );
          $_product-&gt;setBundleSelectionsData ( $bundleSelections );
  
          $_product-&gt;save();
      }catch ( Exception $e ) {
          Mage::log ( $e-&gt;getMessage () );
          echo $e-&gt;getMessage ();
      }
  
      echo 1; exit;
      $_product = null;
  
  }
  </code></pre>
  
  <p>But this gives me following error as </p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`aero_dev`.`catalog_product_bundle_option_value`, CONSTRAINT `FK_CAT_PRD_BNDL_OPT_VAL_OPT_ID_CAT_PRD_BNDL_OPT_OPT_ID` FOREIGN KEY (`option_id`) REFERENCES `catalog_product_bundle_option` (`opt), query was: INSERT INTO `catalog_product_bundle_option_value` (`option_id`, `store_id`, `title`) VALUES (?, ?, ?)
  </code></pre>
  
  <p>Any help would be appreciated. </p>