2

I followed steps in this topic Magento 2 run native product csv import programtically, but it didn't work. When I try to import product through a CSV file in back-office, 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 an error.

I think I didn't upload the CSV file correctly.

I put it in a personal folder, then I copy it in /var/importexport/ renamed as catalog_product.csv. The function importSource seems to work but no product is imported, and the file isn't copied in /var/import_history/.

In my function, I got an argument that is the file path of my CSV file. My code :

$dataImport = 
[
    "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"=>""
];

$newFilvecsv = "/root/var/importexport/catalog_product.csv";
copy($filecsv->getFilepath(), $newFilvecsv);

$importModel = $this->importModelFactory->create();
$importModel->setData($dataImport);

$source = $this->importAdapter->findAdapterFor(
    $newFilvecsv,
    $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT),
    $importModel->getData('_import_field_separator')
);
$isValid = $importModel->validateSource($source);

if ($importModel->getProcessedRowsCount() === 0 || $importModel->getErrorAggregator()->getErrorsCount() || !$isValid){ // Fichier non valide
} 

try {
    $importModel->importSource();
} catch (\Exception $e) {
    $this->logger->info("Import impossible");
} 

return "Import fini";

I'm lost. Can you help me ?

Your Answer

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

Browse other questions tagged or ask your own question.