To create a support ticket, start a search

Product Import: Importing a field in a table (insert and update)

Prerequisites

The field has to be present in the database

 

Target

We would like to import additional information onto a customer from a CSV file to a field in a table. If the information already exists in a table, it will be updated. If the information does not exist yet, it will be created.

 

Setup

To add the field to the list of available fields to import, click on the in the 'Add a field' panel and enter the following information:

What is the field ID?: my_field (has to correspond exactly to the field name in the database)

 

Sc creates the field, you now need to populate the grid with:

ID: my_field
Name: my_field

 

From the Advanced Properties panel on the right handside:

- select the menu Import Process and enter:

if ($switchObject == 'my_field' && !empty($newcustomer->id)) {
   $sql = "SELECT *
           FROM " . DB_PREFIX . "my_table
           WHERE id_customer=" . (int)$newcustomer->id;
   $exist = Db::getInstance()->ExecuteS($sql);
   if (!empty($exist[0]["id_customer"])) {
       $TODO[] = "UPDATE " . DB_PREFIX . "my_table
                   SET " . $switchObject . "='" . pSQL($GLOBALS["import_value"], true) . "'
                   WHERE id_customer=" . (int)$newcustomer->id;
   } else {
       $TODO[] = "INSERT INTO " . DB_PREFIX . "my_table(id_customer, " . $switchObject . ")
                   VALUES ('" . (int)$newcustomer->id . "','" . pSQL($GLOBALS["import_value"], true) . "')";
   }
}

 

You will need to adapt this code to your needs.

 

Exit the editing window.

The new field can now be included in your import mapping.

 




Related articles