How to Import Custom CSV to Magento 2 Programmatically

magento 2 import custom csv programmatically

Let’s discuss the Magento 2 import of custom CSV files. Recently, we’ve been surfing across endless StackExchange discussions and stumbled upon an exciting topic that wasn’t highlighted in our Cookbook. The problem was easy to understand: there was a custom CSV and a user had to import it to Magento 2. The probability of a similar situation for an average e-commerce business is quite high.  Therefore, we’ve decided to narrate it in our blog, adding several more vital facts. So, what is the issue?

Bilal Usean asked the community how to import a custom CSV file to a custom table in Magento 2. Although forum members provided several ways to achieve this goal, we didn’t find any user-friendly solution aimed at people with less technical skills than required by the most rated answer. Variants discussed on StackExchange seem to be highly efficient but too complicated. However, there is one more way to import a custom CSV file to Magento 2 with the minimum headache. Below, we provide the original answer as well as our alternative solution. You will also learn how to import a custom CSV to Magento 2 programmatically. Since some of the provided solutions incorporate the Improved Import & Export extension, we also share its core features after discussing the initial resolution.

How to Import Custom CSV to Magento 2 (Guide for Technical Specialists)

Atish Goswami suggests extending the Magento 2’s default import functionality as follows. First of all, he proposes to register a new custom module. At this point, users with not enough technical skills understand that the provided solution won’t help them unless they ask technical specialists for help. But let’s return to our mutton – the import of a custom CSV.

The new module is called “Custom Import.” You have to create a module.xml file under app/code/Foo/CustomImport/etc/module.xml. Use the following snippet:

Now, you can proceed to the second step on your way to importing a custom CSV file to a custom Magento 2 table. It is necessary to create another new file – import.xml. This time, you should save it under app/code/Foo/CustomImport/etc/import.xml. Use the following snippet:

Where

  • behaviorModel is aimed at the class name of the model class responsible for handling the import logic;
  • label is a label name of the dropdown option;
  • name is a name for the dropdown option value.

What do we have now? The steps described above add a new option to the “Entity Type” dropdown under System->Data Transfer->Import.

magento 2 import custom csv programmatically: select entity type

Unfortunately, it is not the end. You still have to create a model file under app/code/Foo/CustomImport/Model/Import/CustomPricing.php. Use this snippet:

The author of the answer recommends taking the reference of Magento_AdvancedPricingImportExport to get a better understanding of the import logic. What to do next? How do we import a custom CSV file to a custom table in Magento 2? It seems that you can use the standard Magento 2 import tool to do that. 

Unfortunately, this method looks quite complicated. Another similar solution is provided by the author of the question. You can find it described here: Magento 2 programming: implement import with (import.xml). The article offers even more details but looks complicated as hell for non-technical users. If you still have no idea how to transfer a custom CSV to Magento 2, here is our recommendation. 

How to Import Custom CSV to Custom Table in Magento 2 (Guide for Everyone)

If you are a non-technical user, you won’t be able to implement the method described above due to the lack of understanding of various nuances. Luckily, there is a solution that completely replaces the necessity to master coding when you need to import a custom CSV file to a custom Magento 2 table. Furthermore, you get the ability to import files of other formats even if they are packed into an archive. Meet the Improved Import & Export Magento 2 extension – your number one tool when it comes to data transfers. 

With this module, you can easily import any custom CSV file to any Magento 2 table within just a few clicks:

  1. Create a new import profile; 
  2. Choose a schedule of updates;
    magento 2 import custom csv programmatically: general settings
  3. Specify an entity you want to import;
    magento 2 import custom csv programmatically: import settings
  4. Configure import behavior;
    magento 2 import custom csv programmatically: import behavior
  5. Select a file type and source;
    magento 2 import custom csv programmatically: import source
  6. Map third-party attributes to ones used on your e-commerce website. At this step, you prepare your custom CSV to Magento 2 requirements. No matter what column titles your import table has. Now, you can replace them with the required Magento 2 attributes during the import.
    magento 2 import custom csv programmatically: mapping
    Note that you can also edit attribute values at this stage.
  7. Now, save the job, and run the Magento 2 import of a custom CSV file.magento 2 import custom csv programmatically: import run

That’s it! Just do these simple actions, and you are ready. Note that the extension saves and applies created profiles to all further updates. It means that you configure it only once (per entity per integration). Next, Improved Import & Export transfers your custom CSV file to a custom Magento 2 table automatically.  

If you need any customisations – contact our support. We will help you choose the exact solution that suits your business needs and provide several ways of customizing the basic functionality of our plugin.

How to Import Custom CSV to Magento 2 Programmatically

Now, when your import profile is configured, you launch the import of custom CSV files programmatically via CLI. The Improved Import & Export lets you use the following import commands in the Magento 2 root folder:

If you run the import job:run command without a Job ID, ALL the enabled jobs are executed.

That’s it! You configure an import profile suitable for your custom CSV and then launch the Magento 2 import programmatically.

More Features to Improve The Magento 2 Import

Now, let’s take a look at other features that enhance the default import (and export) processes providing the ability to connect to any external system.

The Improved Import & Export Magento 2 extension provides advanced options when it comes to the automation of data transfers. It lets you create cron-based schedules or use triggers to launch import/export processes automatically. Thus, you no longer need to participate in importing custom CSV files to Magento 2. 

magento 2 import automation

Mapping is another feature that enables a custom data file import in Magento 2. With Improved Import & Export, you can apply mapping presets, replacing all third-party attributes with ones used internally in a fully automated mode. Thus, the module edits any custom data file from a third-party system, making it suitable for Magento 2.

Magento 2 mapping presets

If a preset you need is absent, Improved Import & Export offers an interface where you can do everything manually. The procedure is quite simple: you choose a third-party attribute and specify a replacement. 

Besides, you can map and edit attribute values and third-party categories. In addition to that, the Improved Import & Export Magento 2 extension enables you to create attributes on the fly. Even if your custom CSV file lacks some attributes, our module will still transfer it to your Magento 2 website, recreating the missing data.

And, of course, our plugin dramatically extends the number of supported file formats and sources. You can freely leverage API connections and intermediary services to transfer data from third parties.

As for the supported entities, they include:

  • Products;
  • Categories;
  • Customers and Addresses;
  • Advanced Pricing;
  • Product Attributes;
  • Orders;
  • CMS Pages;
  • CMS Blocks;
  • Catalog & Cart Rules;
  • Gift Cards;
  • Reviews;
  • URL Rewrites;
  • Search Terms and Synonyms;
  • Widgets;
  • Page Hierarchy.

It means that you can freely transfer a data file that contains them. Also, note that we add more entities with every new extension update.

In addition to CSV, you can also import:

  • XML
  • Json
  • XLSX
  • ODS
  • ZIP & TAR

With XML and CSV file format support, Improved Import & Export eliminates the necessity to convert tables your suppliers provide. Furthermore, you can import any XML file to Magento 2 with XSLT support. Our module lets you transform XML files into the format appropriate for import procedures based on XSLT instructions.

It is also possible to import Json to Magento 2. This feature enables Rest API transfers and opens further opportunities than the default import tool offers.

The ability to import Excel and OpenOffice formats to Magento 2 incorporates the extension’s support for XSL, XSLX, and ODS tables. If your partner stores the necessary data in Excel and OpenOffice tables, there is no need to convert them to CSV to import different entities to Magento 2. 

Last but not least, the Improved Import & Export Magento 2 extension provides support for ZIP & TAR archives. There is no need to extract a custom CSV table from an archive before importing it to Magento 2. You can import files of the supported formats even when they are compressed.

The module lets you import a custom CSV file to Magento 2 from the following sources:

  • REST and SOAP API;
  • Google Sheets;
  • Direct URL;
  • FTP/SFTP;
  • Dropbox;
  • Direct File Upload.

We will add new sources with further updates. 

You can also enhance the functionality of our module with the help of the following add-ons:

  • MSI Add-on lets you work with entities added to Magento 2.3 with an official Multi-Source Inventory extension. You can freely import and export such data as warehouses and product quantity per the warehouse.
  • B2B Add-on provides the ability to work with entities added to Magento Commerce (Cloud) with an official B2B extension. It means that you can import and export companies, shared catalogs, quotes, negotiable quotes, and requisition lists.
  • WooCommerce Migration Add-on enables seamless migration from WordPress to Magento 2. No programming skills are necessary since the add-on allows importing WooCommerce products, customers, addresses, and orders to Magento 2 within just a few clicks.
  • MS Dynamics Integration Add-on enables the Magento 2 MS Dynamics Business Central ERP/CRM integration. The extension synchronizes products, orders, customers, shipping, and invoices between the two systems.
  • NetSuite Integration Add-on offers similar opportunities to the ones we just described above.
  • Banggood Dropshipping Add-on introduces the easiest ways to start a dropshipping business with Magento 2. The add-on lets you connect your e-commerce store with the Banggood catalog, synchronizing products, orders, shipping details, etc. Now, you can sell products without a warehouse.

You can find more add-ons here: Magento 2 Extensions by Firebear.

Magento 2 Custom File Import & Export FAQ

Let’s take a look at other nuances of importing and exporting custom files to custom tables in Magento 2 no matter what their format is. With the help of the Improved Import & Export Magento 2 extension, you can easily transfer all of them.

How to import and export custom files between Magento 2 custom tables automatically?

If you need to import or export custom files between a custom table in Magento 2 and third parties or other Magento 2 websites regularly, use the Improved Import & Export extension. The module uses a cron scheduler for automating data transfers. Consequently, you get the ability to import and export custom files between Magento 2 custom tables and external destinations automatically. Create a schedule of updates and let the module do everything for you.

How to import custom files with third-party attributes to Magento 2?

Magento 2 blocks the import of third-party data that doesn’t follow its requirements. Thus, the default data transfer solution provides a negative answer: you cannot run such import processes. However, you can use the Improved Import & Export extension to do that. The module lets you create a mapping scheme where you match third-party attributes and values to the corresponding ones used within the system. As a result, any custom file can be easily adapted to the native standards of your e-commerce website even if you use custom tables in your database. Note that you can also apply mapping to export processes to generate any custom output your partners may require, freely transferring custom files to and from Magento 2.

How to import and export custom ODS to Magento 2?

In addition to CSV, Improved Import & Export lets you move custom data via ODS files. You no longer need to convert them to CSV to fit the Magento 2 requirements. You can also export data using this file format.

How to import and export custom XLSX files to Magento 2?

In addition to CSV, Improved Import & Export lets you move custom data via XLSX files. You no longer need to convert them to CSV to fit the Magento 2 requirements. You can also export data using this file format.

How to import and export custom XML files to Magento 2?

In addition to CSV, Improved Import & Export lets you move custom data via XML files. You no longer need to convert them to CSV to fit the Magento 2 requirements. You can also export data using this file format.

How to import and export custom JSON files to Magento 2?

In addition to CSV, Improved Import & Export lets you move custom data via JSON files. You no longer need to convert them to CSV to fit the Magento 2 requirements. You can also export data using this file format.

How to import custom data to Magento 2 via compressed files?

If your custom files are compressed, you can also transfer them to Magento 2 with the help of the Improved Import & Export extension. However, there is a requirement to follow: your file archive should be in a ZIP or TAR format. In addition to CSV, our module also supports compressed XML, JSON, ODS, and XLSX files.

How to import data to Magento 2 from Google Sheets?

If custom data is stored in Google Sheets, it is no longer a problem. There is no need to export it from the service and then transfer the output to Magento 2. The Improved Import & Export lets you move any custom table stored in Google Sheets to your e-commerce website even if you use a custom table in your database. Specify the address of the corresponding Google Sheets table and create a mapping scheme to match unsupported attributes with the ones your website relies upon.

How to import and export custom files to a custom table in Magento 2 from Dropbox?

In addition to Google Sheets, the Improved Import & Export module lets you transfer custom tables to Magento 2 from Dropbox. This time, you can import custom files to a custom table in Magento 2 from the popular file storage. You can transfer CSV, JSON, ODS, XML, or XLSX tables. If it is a compressed file, it should be in a ZIP or TAR format. You can use this file storage to export custom files.

How to import and export custom files to Magento 2 from Google Drive?

In addition to Google Sheets and Dropbox, the Improved Import & Export module lets you leverage Google Drive to transfer custom files to custom Magento 2 databases. You can transfer CSV, JSON, ODS, XML, and XLSX files that can be compressed in a ZIP or TAR format. You can use this file storage to export custom files.

How to import and export custom data to a custom table in Magento 2 via API?

The Improved Import & Export extension lets you avoid using file transfers for updating your Magento 2 database by creating API connections. As a result, it is possible to transfer custom tables between Magento 2 and any external system in a bi-directional data transfer. The module’s import/export jobs provide an interface where you can specify the parameters necessary to sync the two systems. As for transferring unsupported attributes that are often used in custom tables, the plugin lets you leverage the mapping functionality for API connections.

Final Words

As you can see, Improved Import & Export is a very potent tool for transferring data between your Magento 2 website and any external systems. Of course, it can easily import a custom CSV file to a custom Magento 2 table in a very user-friendly way. No coding skills are required! When a corresponding import profile is ready, you can import custom CSVs to Magento 2 programmatically. For further information, follow the link below and contact our support:

Get Improved Import & Export Magento 2 Extension