0

I am trying to use magento 2 native csv import in my custom module. I am using following step to achieve requirement after some R&D but not working for me. I place csv file with sample data at location self::PATH_FILENAME and it works.

Getting issues

1) Unable to create configurable product variations. But when I try same csv from admin it works.

2) Observer event not working catalog_product_import_bunch_save_after. But when I try same csv from admin it works.

public function _importProducts($rowData){
    $csvModel = $this->csvFactory->create(['destination' => self::PATH_FILENAME]);
    $csvModel->writeRow($rowData);

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

    $importModel->setData([
                "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"=>self::PATH_IMAGES
            ]);

    $sourceModel = \Magento\ImportExport\Model\Import\Adapter::findAdapterFor(
                    self::PATH_FILENAME,
                    $this->objectManager->create(\Magento\Framework\Filesystem::class)
                                        ->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::ROOT),
                    $importModel->getData(\Magento\ImportExport\Model\Import::FIELD_FIELD_SEPARATOR)
            );

    $isValid = $importModel->validateSource($sourceModel);
    if ($importModel->getProcessedRowsCount() === 0 || 
        !$isValid || 
        $importModel->getErrorAggregator()->getErrorsCount()
    ) {
        var_dump($importModel->getErrorAggregator()->getAllErrors());
        exit;
    }

    try {
        $importModel->importSource();
    } catch (\Exception $e) {
        throw new \Exception($e->getMessage());
    }
}

Note : I am using command line 'php bin/magento custom:import' for execute.

If you guys have any Idea please share

Any help will be appreciated

  • could you please post exact error? and which place you put the CSV file? – Keyur Shah Apr 29 '19 at 16:57
  • NO error and I put csv file pub/media/customimport – Chander Shekhar Apr 30 '19 at 6:25
  • @ChanderShekhar , Have you found any solution for this? – krupa parikh Aug 16 '19 at 5:53

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.