1

I have been searching for a way of achieving this but so far no luck. I have also looked through the core and found where it is called (vendor\magento\module-import-export\Controller\Adminhtml\Import\Start.php) but can't work out where to define the CSV file to import.

I know I need to pass data to it like so:

Array
(
    [form_key] => 123456789xyz
    [entity] => catalog_product
    [behavior] => append
    [validation_strategy] => validation-stop-on-errors
    [allowed_error_count] => 10
    [_import_field_separator] => ,
    [_import_multiple_value_separator] => ,
    [import_images_file_dir] => 
)

Any one have any hints as to how I can run the native CSV import via code?

Thanks

  • I followed previous steps, but it didnt work. When I try to import product throught a csv file in backoffice, the check data is correct and the import too. But, when I try to use the same file in my custom module, the check data function (validateSource) gives me error. I think I don't upload csv file correctly. I put it in personnal folder, then I copy it in /var/importexport/ rename as catalog_product.csv. The function importSource seems to work but no product is imported, and the file isn't copy in /var/import_history/. I'm lost. Can you help me ? – Fanny Ribard Dec 12 '18 at 16:42
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Rama Chandran M Dec 12 '18 at 17:01
  • If it’s any help I also use magmi, which has a new project running for m2. Works great for me. Give it a google. – sulman Dec 12 '18 at 20:36
3
  1. You need to inject an instance of \Magento\ImportExport\Model\Export\Adapter\CsvFactory into your constructor
  2. Using the CsvFactory, you need to create an instance of the CSV Model: $csvModel = $this->csvFactory->create(['destination' => 'path/to/your/file.csv']);
  3. Using the CSV Model, write out your data: $csvModel->writeRow(['your' => 'data']);
  4. Inject an instance of \Magento\ImportExport\Model\ImportFactory into your constructor
  5. Using the ImportFactory, you need to instantiate the Import Model: $importModel = $this->importModelFactory->create();
  6. Set any data you need to pass to the import model: $importModel->setData(['your' => 'data']);
  7. Get an instance of the CSV source model: $sourceModel = ImportAdapter::findAdapterFor( 'path/to/your/file.csv', $this->filesystem->getDirectoryWrite(DirectoryList::ROOT), $importModel->getData('_import_field_separator') );
  8. Validate your import: $isValid = $importModel->validateSource($sourceModel);
  9. Handle the validation results: if ($importModel->getProcessedRowsCount() === 0 || !$isValid || $importModel->getErrorAggregator()->getErrorsCount()) { // Display or log the error return; }
  10. Run the import: $importModel->importSource();

There's a lot more involved in the error handling, but this should at least get you started.

  • This was an absolute lifesaver. I'm so glad I found this. Thanks!! – Tony DeStefano Oct 27 '17 at 18:25
  • What happened with the variable $csvModel? It is not used after step 3. – F3L1X79 Jun 22 '18 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.