How to add a custom import adapter in Magento 2

- E-Commerce, Fire development, Magento 2

Magento 2 export CLI

StackExchange is always a good source of answers to all possible questions, including ones related to Magento 2. Therefore, we monitor the platform to provide you with the most useful solutions posted there. This time our attention is captured by Mir, who asks about a custom import adapter for Magento 2. The author of the question is not satisfied with the default import opportunities of the platform (and we know why), so let’s find out how to solve the current issue. Find more tips in our Cookbook.

The issue

Mir wants to extend the catalog import to support XML files, which are not supported by default. In this situation, you can to convert them to CSV to enable the import. However, the procedure may be time-consuming.

The author’s adapter supports csv and zip. We also know that the source adapter is available under Magento\ImportExport\Model\Import\Adapter. It uses using the file extension from the factory method:

The challenge is to add another adapter type. Mir reports that adding preference is not helpful. The class itself is a copy-paste from Magento 1.

The solution for tech-savvy users

KAndy recommends creating a class in namespace Magento\ImportExport\Model\Import\Source for a quick workaround. He also informs that it is necessary to declare this class in the same way as registration.php to autoload it. Use the following code to do that:

The solution with no coding

Here at Firebear, we recommend you not to reinvent the wheel and use the existing solution. You may argue that the market is full of import extensions that are not very reliable. Still, we always want to improve the daily routine of our readers by highlighting the best modules in the ecosystem. So, meet the Improved Import & Export plugin for Magento 2.

When it comes to file formats, our extension supports not only CSV but also XML, JSON, XLSX, and ODS. Besides, you can import all of them as ZIP & TAR archives. Thus, you no longer need a custom Magento 2 import adapter to transfer XML files into your website. 

It is also important that Improved Import & Export lets you transfer data files from the following sources:

  • Direct URL;
  • Dropbox;
  • Direct File Upload.

And you can easily avoid default file transfers since the module enables you to leverage REST and SOAP APIs as well as Google Sheets.

Besides, the Improved Import & Export Magento 2 module can automate all import and export processes. Thus, the necessity to create a custom import plugin to move datafiles automatically is eliminated. You can configure cron-based schedules or choose triggers to launch import/export processes within our extension. In the case of schedules, you can select a predefined timespan or create a custom interval. The configuration is illustrated below: 

Magento 2 export CLI

Another feature that dramatically extends the default possibilities of the platform is mapping. The Improved Import & Export extension offers a bunch of tools that help you match third-party values to ones used in Magento 2. For instance, you can replace third-party or custom attributes in the backend within a few clicks. The Improved Import & Export module offers mapping presets to let you achieve this goal. You select a platform to import data from, and the plugin replaces all third-party attributes with ones used internally. The corresponding configuration looks as follows:

Magento 2 export CLI

At the same time, it is possible to match attributes manually. The Improved Import & Export extension provides every import and export profile with a dedicated section where you can do that. Thus, it is possible to match all third-party and custom attributes to ones used internally manually without working with data filed directly. Select a third-party attribute and specify a replacement in the same row. 

Other mapping improvements include the ability to map and edit attribute values and map third-party categories. Besides, our module lets you create attributes on the fly if data files lack essential information. The extension generates the missing data based on the provided parameters.

Final Words

As you can see, adding a custom import adapter in Magento 2 requires technical skills and coding experience. However, you can get the desired functionality without any programming knowledge. You only need to install the Improved Import & Export Magento 2 extension. If our module lacks features you need, contact us, and we will discuss its customization. Follow the link below for further information:

Get Improved Import & Export Magento 2 Extension