2

I have rewrite \Magento\ImportExport\Model\Import class with function:

 protected function _getSourceAdapter($sourceFile)
{
    return 
         \Namespace\DownloadableImport\Model\Import\Adapter::findAdapterFor(
        $sourceFile,
        $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT),
        $this->getData(self::FIELD_FIELD_SEPARATOR)
    );
}
i have replce adapter 

Magento\ImportExport\Model\Import\Adapter with \Namespace\DownloadableImport\Model\Import\Adapter

but when i uploading csv and check data process it's give me error but if i replace original adapter it works fine

my custom adapter code:

namespace Namespace\DownloadableImport\Model\Import;

use Magento\Framework\Filesystem\Directory\Write;


 class Adapter 
{
/**
 * Adapter factory. Checks for availability, loads and create instance of import adapter object.
 *
 * @param string $type Adapter type ('csv', 'xml' etc.)
 * @param Write $directory
 * @param string $source
 * @param mixed $options OPTIONAL Adapter constructor options
 *
 * @return AbstractSource
 *
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public static function factory($type, $directory, $source, $options = null)
{
    if (!is_string($type) || !$type) {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('The adapter type must be a non-empty string.')
        );
    }
   // $adapterClass = 'Magento\ImportExport\Model\Import\Source\\' . ucfirst(strtolower($type));
    $adapterClass = 'Namespace\DownloadableImport\Model\Import\Source\\' . ucfirst(strtolower($type));

    if (!class_exists($adapterClass)) {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('\'%1\' file extension is not supported', $type)
        );
    }
    $adapter = new $adapterClass($source, $directory, $options);

    if (!$adapter instanceof AbstractSource) {
          throw new \Magento\Framework\Exception\LocalizedException(
            __('Adapter must be an instance of \Magento\ImportExport\Model\Import\AbstractSource')
        );
    } 


    return $adapter;
}

/**
 * Create adapter instance for specified source file.
 *
 * @param string $source Source file path.
 * @param Write $directory
 * @param mixed $options OPTIONAL Adapter constructor options
 *
 * @return AbstractSource
 */
public static function findAdapterFor($source, $directory, $options = null)
{
    return self::factory(pathinfo($source, PATHINFO_EXTENSION), $directory, $source, $options);
}

if (!$adapter instanceof AbstractSource) { throw new \Magento\Framework\Exception\LocalizedException( __('Adapter must be an instance of \Magento\ImportExport\Model\Import\AbstractSource') ); } if i am removing this piece of code it's working fine My custom csv code:

<?php

/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Namespace\DownloadableImport\Model\Import\Source;

   class Csv extends \Magento\ImportExport\Model\Import\AbstractSource
  {

   const COL_ATTR_SET="attribute_set_code";
   const VAL_COL_TYPE="downloadable";
   const VAL_ATTR_SET="Migration_Default";

 /**
 * @var \Magento\Framework\Filesystem\File\Write
 */
protected $_file;

/**
 * Delimiter.
 *
 * @var string
 */
protected $_delimiter = ',';

/**
 * @var string
 */
protected $_enclosure = '';



public function __construct(
    $file,
    \Magento\Framework\Filesystem\Directory\Read $directory,
    $delimiter = ',',
    $enclosure = '"'

) {
    register_shutdown_function([$this, 'destruct']);
    try {
        $this->_file = $directory->openFile($directory->getRelativePath($file), 'r');
    } catch (\Magento\Framework\Exception\FileSystemException $e) {
        throw new \LogicException("Unable to open file: '{$file}'");
    }
    if ($delimiter) {
        $this->_delimiter = $delimiter;
    }
    $this->_enclosure = $enclosure;
    $this->_counter = 0;
    parent::__construct($this->_getNextRow());
}

protected function _getNextRow()
{
    $parsed = $this->_file->readCsv(0, $this->_delimiter, $this->_enclosure);
    if (is_array($parsed) && count($parsed) != $this->_colQty) {
         $counter = $this->_counter;
        if($counter == 0)
        {
            $parsed[]= \Magento\CatalogImportExport\Model\Import\Product::COL_TYPE;
            $parsed[]= \Magento\CatalogImportExport\Model\Import\Product::COL_ATTR_SET;
            $parsed[]= self::COL_ATTR_SET;    
        }
        elseif($counter > 0){
            $parsed[]= self::VAL_COL_TYPE;
            $parsed[]= self::VAL_ATTR_SET;
            $parsed[]= self::VAL_ATTR_SET;       
        }

    $this->_counter = ++$counter;
        foreach ($parsed as $element) {
            if (strpos($element, "'") !== false) {
                $this->_foundWrongQuoteFlag = true;
                break;
            }
        }
    } else {
        $this->_foundWrongQuoteFlag = false;
    }


    return is_array($parsed) ? $parsed : [];
}
public function destruct()
{
    if (is_object($this->_file)) {
        $this->_file->close();
    }
}
 public function rewind()
{
    $this->_file->seek(0);
    $this->_getNextRow();
    // skip first line with the header
    parent::rewind();
}

}

14
  • What error that give? can you write that. Commented Jul 27, 2018 at 11:24
  • The file "D:/Projects/xampp/htdocs/m2/var/importexport/catalog_product.csv" cannot be deleted Warning!unlink(D:/Projects/xampp/htdocs/m2/var/importexport/catalog_product.csv): Permission denied Commented Jul 27, 2018 at 11:26
  • Can you write whole error log trace because above is warning and you have written unlink ? Commented Jul 27, 2018 at 11:31
  • yes this warning is occur due to custom adapter when i replace it with original one it's not giving any warning Commented Jul 27, 2018 at 11:33
  • can you check header name is proper in csv file may this message is misleading as per: github.com/magento/magento2/issues/6658 Commented Jul 27, 2018 at 11:46

1 Answer 1

1

In file

Namespace\DownloadableImport\Model\Import\Adapter

if (!$adapter instanceof AbstractSource) replace with if (!$adapter instanceof Magento\ImportExport\Model\Import\AbstractSource)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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