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/createAttributeAndSetAttribute.php

<?php
/*$fileName = './csv/vino.csv';
$attributeSetName = 'Vino';
$attributeSetId = 9; // Check Attribute Set is created
$attributeGroupId = 31; // Check Group is created in Attribute Set - Use script getGroupidOfAttributeSet.php to get Id
*/
$fileName = './csv/formaggio.csv';
$attributeSetName = 'Formaggio';
$attributeSetId = 13; // Check Attribute Set is created
$attributeGroupId = 48; // Check Group is created in Attribute Set - Use script getGroupidOfAttributeSet.php to get Id

$attributeDefault = [
    "attribute" => [
        "is_wysiwyg_enabled" => false,
        "is_html_allowed_on_front" => false,
        "used_for_sort_by" => false,
        "is_filterable" => false,
        "is_filterable_in_search" => false,
        "is_used_in_grid" => false,
        "is_visible_in_grid" => false,
        "is_filterable_in_grid" => false,
        "position" => 0,
        "apply_to" => [],
        "is_searchable" => "0",
        "is_comparable" => "1",
        "is_used_for_promo_rules" => "0",
        "is_visible_on_front" => "0",
        "used_in_product_listing" => "0",
        "is_visible" => true,
        "scope" => "global",
        "attribute_code" => "indaco_default",
        "frontend_input" => "text",
        "entity_type_id" => "4",
        "is_required" => false,
        "is_user_defined" => true,
        "default_frontend_label" => "INDACO DEFAULT",
        "frontend_labels" => null,
        "backend_type" => "int",
        "source_model" => "Magento%5C%5CEav%5C%5CModel%5C%5CEntity%5C%5CAttribute%5C%5CSource%5C%5CTable",
        "default_value" => "",
        "is_unique" => "0"
    ]
];

$attributeDefaultText = [
    "attribute" => [
        "is_visible_in_advanced_search" => "1",

    ]
];

//require("./connection.php");
require("./RestConnect.php");
$api = new Api();

$row = 1;
$header = [];
// flag if is present column with name 'import': imported only row with value import=1
$import = -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; }
            }
        } else {
            if ($import==-1 || $data[$import]==1) {
                for ($c=0; $c < $num; $c++) {
                    if ($import<>$c && strlen(trim($data[$c]))>0) {
                        if (substr($data[$c], 0, 1) === '[' && substr($data[$c], -1) === ']') {
                            $attributeCustom['attribute'][$header[$c]] = json_decode($data[$c], true);
                        } else {
                            $attributeCustom['attribute'][$header[$c]] = trim($data[$c]);
                        }
                    }
                }
                echo var_dump(json_encode($attributeCustom))."\n\n";
                $response = $api->senddata("/rest/V1/products/attributes", $attributeCustom);
                $response = json_decode($response, true);
                if (array_key_exists("attribute_code", $response)) {
                    $attributeSet = '{"attributeSetId": ' . $attributeSetId . ', "attributeGroupId": ' . $attributeGroupId . ', "attributeCode": "' . $response['attribute_code']. '", "sortOrder": 0 }';
                    print $attributeSet."\n";
                    $api->senddata("/rest/V1/products/attribute-sets/attributes", $attributeSet);
                } else {
                    echo " >>>>>>>> ".var_dump($response);
                }
            }
        }
        $row++;
    }
    fclose($handle);
}