To create a support ticket, start a search

Product Import: Importing a language field in a table


Prerequisites

The field has to be present in the database

 

Target

We would like to import a language field in a table, language that will be selected in the import mapping beforehand.

 

Setup

To add the field to the list of available fields to import, click on the add_dis.png 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 table)

 

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 Combinations Process and enter:

if ($switchObject == 'my_field' && $GLOBALS['import_value'] !== '') {
    $value = $GLOBALS['import_value'];
    $id_lang = (int)$getIDlangByISO[$mappingData['CSV2DBOptions'][$firstLineData[$key]]];
    if (!empty($id_lang)) {
        $id_prd = (!empty($id_product) ? (int)$id_product : (int)$newprod->id);
        $sql = "SELECT COUNT(*) FROM " . _DB_PREFIX_ . "mytable_lang
                WHERE id_product=" . (int)$id_prd . "
                AND id_lang = " . (int)$id_lang;
        $count_row = (int)Db::getInstance()->getValue($sql);
        if ($count_row > 0) {
            $TODO[] = "UPDATE " . _DB_PREFIX_ . "mytable_lang
                        SET `" . $switchObject . "`='" . psql($value) . "'
                        WHERE id_product=" . (int)$id_prd . "
                        AND id_lang = " . (int)$id_lang;
        } else {
            $TODO[] = "INSERT INTO " . _DB_PREFIX_ . "mytable_lang (id_product, id_lang, `" . $switchObject . "`)
                        VALUES (" . (int)$id_prd . "," . (int)$id_lang . ",'" . psql($value) . "')";
        }
    }
} 

 

IMPORTANT NOTE: for tables product_lang product_attribute_lang, do not add the following to the above:

else {
    $TODO[] = "INSERT INTO " . _DB_PREFIX_ . "mytable_lang (id_product, id_lang, " . $switchObject . ")
                VALUES (" . (int)$id_prd . "," . (int)$id_lang . ",'" . psql($value) . "')";
}

 

- select the menu Selectable Language and add the field name (separated by a comma if you need to add multiple fields)

Example: last_field,my_field

 

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