Your IP : 216.73.216.189


Current Path : /var/www/magento.test.indacotrentino.com/www/scripts/import/OLD/
Upload File :
Current File : /var/www/magento.test.indacotrentino.com/www/scripts/import/OLD/createCategories.php

<?php
$fileName = './csv/categories.csv';
$categories['root'] = 2;

$attributeDefault = [
    "category" => [

    ]
];

require("./connection.php");
$row = 1;
$header = [];
// flag if is present column with name 'import': imported only row with value import=1
$import = -1;
$nameCategory = -1;
if (($handle = fopen($fileName, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $attributeCustom = $attributeDefault;
        $num = count($data);
        if ($row == 1) {
            for ($c=0; $c < $num; $c++) {
                $header[$c] = trim($data[$c]);
                if (trim($data[$c]) == 'import') { $import = $c; }
                if (trim($data[$c]) == 'name') { $nameCategory = $c; }
            }
        } else {
            if ($import==-1 || $data[$import]==1) {
                for ($c = 0; $c < $num; $c++) {
                    if ($import<>$c && strlen(trim($data[$c]))>0) {
                        if ($header[$c] === 'parent_name') {
                            if (array_key_exists(trim($data[$c]), $categories)) {
                                $attributeCustom['category']['parent_id'] = $categories[trim($data[$c])];
                            } else {
                                die ('ERROR: Missing parent_name category: '.trim($data[$c])."\n\n");
                            }
                        } else {
                            $attributeCustom['category'][$header[$c]] = trim($data[$c]);
                        }
                    }
                }
                $response = senddata("/rest/V1/categories", json_encode($attributeCustom));
                $category = json_decode($response, true);
                if (array_key_exists('id', $category)) {
                    $categories[trim($data[$nameCategory])] = $category['id'];
                } else {
                    die ("Error: Problem with category " . trim($data[$nameCategory]));
                }
            }
        }
        $row++;
    }
    fclose($handle);
}