0

I want to add some attributes to customers, I was able to add one attribute at a time but wanted to add several, but the magento returns me error, what am I doing wrong?

ERROR: Source model "" not found for attribute "type"

mysql4-install.php

<?php
$installer = $this;

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute("customer", "type",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Tipo de Cadastro",
    "input"    => "select",
    "source"   => "",
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "cpf",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "CPF",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "company_name",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Razão Social",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "trading_name",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Nome Fantasia",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "cnpj",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "CNPJ",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "nickname",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Apelido",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "occupation",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Profissão",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "foundation_day",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Data de Fundação da Empresa",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "department",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Departamento",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

$installer->addAttribute("customer", "how_find_us",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Onde conheceu a Flores Online",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,

        ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute(
        "customer", "type",
        "customer", "cpf",
        "customer", "company_name",
        "customer", "trading_name",
        "customer", "cnpj",
        "customer", "nickname",
        "customer", "occupation",
        "customer", "foundation_day",
        "customer", "department",
        "customer", "how_find_us");


$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'type',
    '999',
    'cpf',
    '998',
    'company_name',
    '997',
    'trading_name',
    '996',
    'cnpj',
    '995',
    'nickname',
    '994',
    'occupation',
    '993',
    'foundation_day',
    '992',
    'department',
    '991',
    'how_find_us',
    '990'
);

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
//$used_in_forms[]="checkout_register";
//$used_in_forms[]="customer_account_create";
//$used_in_forms[]="customer_account_edit";
//$used_in_forms[]="adminhtml_checkout";
      $attribute->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100)
                ;
        $attribute->save();



$installer->endSetup();

Config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Teste_Essevai>
                <version>0.1.0</version>
            </Teste_Essevai>
        </modules>
        <global>
            <resources>
                <teste_essevai_setup>
                    <setup>
                        <module>Teste_Essevai</module>
                        <class>Mage_Customer_Model_Entity_Setup</class>
                    </setup>
                </teste_essevai_setup>
            </resources>
        </global>
    </config>

2 Answers 2

1

getAttribute() and addAttributeToGroup() are only meant to work on one attribute at a time. So I'd put the codes in an array and loop through each one, like so:

$attributeCodes = array(
    'type'=>999,
    'cpf'=>998,
    'company_name'=>997,
    'trading_name'=>996,
    'cnpj'=>995,
    'nickname'=>994,
    'occupation'=>993,
    'foundation_day'=>992,
    'department'=>991,
    'how_find_us'=>990
);


$used_in_forms=array('adminhtml_customer');

foreach($attributeCodes as $code=>$sort_order) {
    $attribute = Mage::getSingleton("eav/config")->getAttribute('customer', $code);
    $setup->addAttributeToGroup(
        $entityTypeId,
        $attributeSetId,
        $attributeGroupId,
        $attribute->getId(),
        $sort_order
    );
    $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);
    $attribute->save();
}
15
  • dont work this. Oct 31, 2017 at 18:47
  • Hmm. Try removing source then for your attributes. Does it work or give a new error?
    – Joe
    Oct 31, 2017 at 18:51
  • No, I believe it could be in concatenation done in $ attribute Oct 31, 2017 at 18:56
  • getAttribute() and addAttributeToGroup() are only meant to accept one attribute at a time.
    – Joe
    Oct 31, 2017 at 19:03
  • I'd put the codes in an array and iterate through them. For example: foreach($attribute_codes as $code) { $attribute = Mage::getSingleton('eav/config')->getAttribute(...); $setup->addAttributeToGroup(...); etc... }
    – Joe
    Oct 31, 2017 at 19:06
0

I will post my functional code so that other people can do the same, it creates all the necessary attributes that I need be it text or select. I will also post the code to customer address

local/your/module/etc/config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Your_Module>
                <version>0.1.0</version>
            </Your_Module>
        </modules>
        <global>
            <resources>
                <your_module_setup>
                    <setup>
                        <module>Customers_Atributos</module>
                        <class>Mage_Customer_Model_Entity_Setup</class>
                    </setup>
                </your_module_setup>
            </resources>
        </global>
    </config>

local/your/module/sql/your_module_setup/install.0.1.0.php

I use this first sql installation just for it to recognize the module in core_resource, I had a problem with that, so if you want to follow the same one so that there are no errors. I do not put anything inside the file, it's just for the magento to recognize the module.

This is the first file responsible for creating attributes of customers local/your/module/sql/your_module_setup/mysql4-upgrade-0.1.0-0.1.1.php

Here is an example of each type of input, text and select, if you want to create more than 1 just replicate the field and change what you want, and add the nameattribute in the variable $attributeCodes. To do the same with the address table just change the code with the added comments

 <?php
        $installer = $this;
        $installer->startSetup();

        //Follow the comments if you want to add attributes to customers address




        //Change  $installer->addAttribute("customer", "nameattribute", ... to  $installer->addAttribute("customer_address", "nameattribute", 
        $installer->addAttribute("customer", "nameattribute",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Here is the label that will appear",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default" => "",
            "frontend" => "",
            "unique"     => false)
        );

            $installer->addAttribute("customer", "nameattributeselect",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "Here is the label that will appear",
            "input"    => "select",
            "source"   => "",
            "visible"  => true,
            "required" => false,
            "default" => "",
            "frontend" => "",
            "unique"     => false,
            "source" => "eav/entity_attribute_source_table",
            "option" => array('values' => array('Value 1', 'Value 2')) 
            ));

            $attributeCodes = array(
                'nameattribute',
                'nameattributeselect'
            );


        foreach($attributeCodes as $code) {
        //Change ->getAttribute('customer', $code); to ->getAttribute('customer_address', $code); 
        $attribute = Mage::getSingleton("eav/config")->getAttribute('customer', $code);

        //Change array('adminhtml_customer'); to array('adminhtml_customer_address'); 
        $used_in_forms=array('adminhtml_customer');

        $attribute->setData("used_in_forms", $used_in_forms)
                    ->setData("is_used_for_customer_segment", true)
                    ->setData("is_system", 0)
                    ->setData("is_user_defined", 1)
                    ->setData("is_visible", 1)
                    ->setData("sort_order", 100);
                $attribute->save();
            }

        $installer->endSetup();

After doing all these steps just change the version of the module in config.xml to the version of the file you created in sql, then just refresh the page of the site, clear the cache and add a new client with its attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.