Magento – Magento2: how to add a custom import adapter

importmagento2

I want to extend the catalog import to support xml files. At this time it only supports csv and zip.

The source adapter is detected inside Magento\ImportExport\Model\Import\Adapter using file extension in the factory method:

$adapterClass = 'Magento\ImportExport\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);

How can I add another adapter type?

At first I tought that a preference could be the right way, but preferences can't be used in this case because you can only specify preferences for injectable classes and this isn't injectable.

Actually this class is essentially a copy paste from Magento 1 Mage_ImportExport_Model_Import_Adapter class.

If I can't rewrite this class, can't use a preference or a plugin to extend this class functionality or functionalities of other classes related to the file type adapter, how to achieve that?

Sure I'm missing something obvious.

Best Answer

That's for sure a bug, please create issue on github.

For quick workaround you need create class in namespace Magento\ImportExport\Model\Import\Source. To autoload this class you should declare it the same as registration.php

"autoload": {
    "files": [
        "registration.php",
         "path/to/XmlImportSoure.php"
    ],
Related Topic