Discussions

How can I execute commerce feeds Imports programmatically?

Hi,

I would like to trigger one (already configured) import after the other - programmatically - from e.g. a custom form or module.
Could anyone point me into the right direction?

Thank you.

PS: Commerce is a wonderful module!

Posted: Nov 2, 2011

Comments

maxilein on November 8, 2011

Found somewhere else (sorry, I forgot):

//  $title Title to show to user when executing batch.
//  $method Method to execute on importer; one of 'import', 'clear' or 'expire'.
//  $importer_id Identifier of a FeedsImporter object.
//  $feed_nid If importer is attached to content type, feed node id identifying the source to be imported.
   
// Files to import in specific order.
  $files = array(
    'machine_name_of_feeds_import_job' => 'filetoimport.csv',
    // add more jobs here to import
  );
  $noofimports = count($files);

  foreach ($files as $feed => $file) {
   
    $feedSource = feeds_source($feed);
    // Set the file name to import
    $filename = 'sites/default/files/feeds/' . $file;
    if (!file_destination($filename, FILE_EXISTS_ERROR)) {
      $config = $feedSource->getConfig();
      $config['FeedsFileFetcher']['source'] = $filename;
      $feedSource->setConfig($config);
      $feedSource->save();

      $batch = array(
        'title' => t('Importing: (' .$noofimports. ') ' .$feed ),
        'operations' => array(
          array('feeds_batch', array('import', $feed, 0)),
        ),
        'progress_message' => t('Current: @current | Remaining:
          @remaining | Total: @total | Percentage: @percentage | Estimate:
          @estimate | Elapsed: @elapsed'),
      );
      batch_set($batch);
    }
  }
  batch_process('redirect-url');