Adding new fields to the PrestaShop CSV Importer

The CSV import PrestaShop natively comes with becomes useless if we have added custom fields to our objects. Let’s see how to overcome the limit, and enable extra fields in the CSV Importer as well.

Adding an extra field to PrestaShop objects

Before doing anything with CSVs or the importer, we need a field to fill with your data. If you don’t know how to add one, here is a tutorial on How to extend PrestaShop Objects, and add new fields to them.
In this example, we will use that extrafield one added in the tutorial.
As a side note, it’s not strictly necessary to add the extra field to the product object, as we will see, but it’s essential to have a database column we can use.

Two approaches

Given that the Product class override is not strictly necessary, we can proceed in two ways, depending on our needs: the first, is to complete the previous tutorial, and add the field to the Product Object; the second is to stop after adding the database column, and override an extra method of the AdminImportController, to manually update the new field. We will cover both to see how they work.

The AdminImportController override

Let’s start playing with the real file. First of all, create a new file in override/controllers/admin, and name it AdminImportController.php

Open it up, and add some standard override code within PHP tags

class AdminImportController extends AdminImportControllerCore
{
}

Next, we want to tell the Importer we have an extra field for products. To do this, we have to override the whole construct method, since it’s the one defining available fields for all entities. It’s too long to copy it here, to make sure you grab yours from the original Controller, and drop it into the override.

Save, then reach the cache folder and delete class_index.php, so that the override file is loaded.

If you added the Product class override, you can skip to Testing The Import

Otherwise, there is a little snippet you need to add. Unfortunately, in order to do it we have to override the whole ProductImport() method. Therefore, copy and paste yours into the override file we created; then, reach the very end of it, where it reads: