开发者

Magento - Add attribute to customer entity

For two days I have been trying to add new customer attribute to the magento database. But it isn't working. I added a simple nickname input field to the register.phtml and edit.phtml following this tutorial:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields

Here is the inherent forum post:

http://www.magentocommerce.com/boards/viewthread/9620/

The attribute is added to the database table eav_attribute, but the value doesn't appear in the customer_entity_varchar.


Here is what I have:

All files are located under app/code/local/Company/


app/code/local/Company/Customer/etc/config.xml

(watch out for: <!-- nickname -->)

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Company_Customer>
            <version>0.1.0</version>
        </Company_Customer>
    </modules>
    <admin>
        <fieldsets>
            <customer_dataflow>
                <store_id><system>1</system></store_id>
                <store><ignore>1</ignore></store>
                <entity_id><system>1</system><ignore>1</ignore></entity_id>
                <attribute_set_id><system>1</system><ignore>1</ignore></attribute_set_id>
                <attribute_set><ignore>1</ignore></attribute_set>
                <entity_type_id><system>1</system></entity_type_id>
                <increment_id><system>1</system><ignore>1</ignore></increment_id>
                <parent_id><system>1</system></parent_id>
                <created_at><system>1</system></created_at>
                <updated_at><system>1</system></updated_at>
                <group_id><ignore>1</ignore></group_id>
                <type_id><system>1</system><ignore>1</ignore></type_id>
                <type><ignore>1</ignore></type>
                <created_in><system>1</system><ignore>1</ignore></created_in>
                <website_id><system>1</system></website_id>
                <default_billing><system>1</system></default_billing>
                <default_shipping><system>1</system></default_shipping>
 
                <prefix><billing>1</billing><shipping>1</shipping><mapped>1</mapped></prefix>
                <firstname><billing>1</billing><shipping>1</shipping><required>1</required><mapped>1</mapped></firstname>
                <middlename><billing>1</billing><shipping>1</shipping><mapped>1</mapped></middlename>
                <lastname><billing>1</billing><shipping>1</shipping><required>1</required><mapped>1</mapped></lastname>
                <suffix><billing>1</billing><shipping>1</shipping><mapped>1</mapped></suffix>
                <street_full><billing>1</billing><shipping>1</shipping></street_full>
                <street1><street>1</street></street1>
                <street2><street>1</street></street2>
                <street3><street>1</street></street3>
                <street4><street>1</street></street4>
                <street5><street>1</street></street5>
                <street6><street>1</street></street6>
                <street7><street>1</street></street7>
                <street8><street>1</street></street8>
                <city><billing>1</billing><shipping>1</shipping></city>
                <region><billing>1</billing><shipping>1</shipping></region>
                <country><billing>1</billing><shipping>1</shipping><billing_required>1</billing_required><shipping_required>1</shipping_required></country>
                <postcode><billing>1</billing><shipping>1</shipping><billing_required>1</billing_required><shipping_required>1</shipping_required></postcode>
                <telephone><billing>1</billing><shipping>1</shipping></telephone>
                <company><billing>1</billing><shipping>1</shipping></company>
                <fax><billing>1</billing><shipping>1</shipping></fax>
                <mobile><billing>1</billing><shipping>1</shipping></mobile>
                <!-- nickname -->
                <nickname><system>1</system></nickname>
            </customer_dataflow>
        </fieldsets>
    </admin>
    <global>
        <fieldsets>
            <customer_account>
                <prefix><create>1</create><update>1</update><name>1</name></prefix>
                <firstname><create>1</create><update>1</update><name>1</name></firstname>
                <middlename><create>1</create><update>1</update><name>1</name></middlename>
                <lastname><create>1</create><update>1</update><name>1</name></lastname>
                <suffix><create>1</create><update>1</update><name>1</name></suffix>
                <email><create>1</create><update>1</update></email>
                <password><create>1</create></password>
                <confirmation><create>1</create></confirmation>
                <dob><create>1</create><update>1</update></dob>
                <taxvat><create>1</create><update>1</update></taxvat>
                                 <!-- nickname -->
                <nickname><create>1</create><update>1</update></nickname>
            </customer_account>
        </fieldsets>
 
        <customer>
            <address>
                <formats>
                    <text translate="title" module="customer">
                        <title>Text</title>
                        <defaultFormat><![CDATA[
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
{{depend company}}{{var company}}{{/depend}}
{{var street1}}
{{depend street2}}{{var street2}}{{/depend}}
{{depend street3}}{{var street3}}{{/depend}}
{{depend street4}}{{var street4}}{{/depend}}
{{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}
{{var country}}
T: {{var telephone}}
{{depend fax}}F: {{var fax}}{{/depend}}
{{depend mobile}}M: {{var mobile}}{{/depend}}
                        ]]></defaultFormat>
                    </text>
                    <oneline translate="title" module="customer">
                        <title>Text One Line</title>
                        <htmlEscape>true</htmlEscape>
                        <defaultFormat>
<![CDATA[{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}, {{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}]]>
                        </defaultFormat>
                    </oneline>
                    <html translate="title" module="customer">
                        <title>HTML</title>
                        <htmlEscape>true</htmlEscape>
                        <defaultFormat><![CDATA[
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
{{depend company}}{{var company}}<br />{{/depend}}
{{var street1}}<br />
{{depend street2}}{{var street2}}<br />{{/depend}}
{{depend street3}}{{var street3}}<br />{{/depend}}
{{depend street4}}{{var street4}}<br />{{/depend}}
{{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}<br/>
{{var country}}<br/>
{{depend telephone}}T: {{var telephone}}{{/depend}}
{{depend fax}}<br/>F: {{var fax}}{{/depend}}
{{depend mobile}}<br/>M: {{var mobile}}{{/depend}}
            ]]></defaultFormat>
                    </html>
                    <pdf translate="title" module="customer">
                        <title>PDF</title>
                        <defaultFormat><![CDATA[
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
{{depend company}}{{var company}}|{{/depend}}
{{var street1}}|
{{depend street2}}{{var street2}}|{{/depend}}
{{depend street3}}{{var street3}}|{{/depend}}
{{depend street4}}{{var street4}}|{{/depend}}
{{depend city}}{{var city}},  {{/depend}}{{depend region}}{{var region}}, {{/depend}}{{var postcode}}|
{{var country}}|
{{depend telephone}}T: {{var telephone}}{{/depend}}|
{{depend fax}}<br/>F: {{var fax}}{{/depend}}|
{{depend mobile}}<br/>M: {{var mobile}}{{/depend}}|
            ]]></defaultFormat>
                    </pdf>
                    <js_template template="title" module="customer">
                        <title>Javascript Template</title>
                        <defaultFormat><![CDATA[#{prefix} #{firstname} #{middlename} #{lastname} #{suffix}<br/>#{company}<br/>#{street0}<br/>#{street1}<br/>#{street2}<br/>#{street3}<br/>#{city}, #{region}, #{postcode}<br/>#{country_id}<br/>T: #{telephone}<br/>F: #{fax}<br/>M: #{mobile}]]></defaultFormat>
                    </js_template>
                </formats>
            </address>
        </customer>
        <models>
            <customer_entity>
                <rewrite>
                    <setup>Company_Customer_Model_Entity_Setup</setup>
                </rewrite>
            </customer_entity>
        </models>
        <resources>
            <customer_setup>
                <rewrite>
                    <setup>Company_Customer_Model_Entity_Setup</setup>
                </rewrite>
            </customer_setup>
        </resources>
    </global>
</config>

app/code/local/Company/Customer/Model/Entity/Setup.php

(watch out again for: /* nickname */)

class Company_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup{
            
    public function getDefaultEntities()
        {
            return array(
                'customer' => array(
                    'entity_model'          =>'customer/customer',
                    'table'                 => 'customer/entity',
                    'increment_model'       => 'eav/entity_increment_numeric',
                    'increment_per_store'   => false,
                    'additional_attribute_table' => 'customer/eav_attribute',
                    'entity_attribute_collection' => 'customer/attribute_collection',
                    'attributes' => array(
    //                    'entity_id'         => array('type'=>'static'),
    //                    'entity_type_id'    => array('type'=>'static'),
    //                    'attribute_set_id'  => array('type'=>'static'),
    //                    'increment_id'      => array('type'=>'static'),
    //                    'created_at'        => array('type'=>'static'),
    //                    'updated_at'        => array('type'=>'static'),
    //                    'is_active'         => array('type'=>'static'),
    
                        'website_id' => array(
                            'type'          => 'static',
                            'label'         => 'Associate to Website',
                            'input'         => 'select',
                            'source'        => 'customer/customer_attribute_source_website',
                            'backend'       => 'customer/customer_attribute_backend_website',
                            'sort_order'    => 10,
                        ),
                        'store_id' => array(
                            'type'          => 'static',
                            'label'         => 'Create In',
                            'input'         => 'select',
                            'source'        => 'customer/customer_attribute_source_store',
                            'backend'       => 'customer/customer_attribute_backend_store',
                            'visible'       => false,
                            'sort_order'    => 20,
                        ),
                        'created_in' => array(
                            'type'          => 'varchar',
                            'label'         => 'Created From',
                            'sort_order'    => 30,
                        ),
                        'prefix' => array(
                            'label'         => 'Prefix',
                            'required'      => false,
                            'sort_order'    => 37,
                        ),
                        'firstname' => array(
                            'label'         => 'First Name',
                            'sort_order'    => 40,
                        ),
                        'middlename' => array(
                            'label'         => 'Middle Name/Initial',
                            'required'      => false,
                            'sort_order'    => 43,
                        ),
                        'lastname' => array(
                            'label'         => 'Last Name',
                            'sort_order'    => 50,
                        ),
                        'suffix' => array(
                            'label'         => 'Suffix',
                            'required'      => false,
                            'sort_order'    => 53,
                        ),
                        'email' => array(
                            'type'          => 'static',
                            'label'         => 'Email',
                            'class'         => 'validate-email',
                            'sort_order'    => 60,
                        ), /* nickname */
                        'nickname' => array(
                            'label'         => 'nickname',
                            'unique'        => true,
                            'sort_order'    => 65,
                            'required'      => true,
                        ),
                        'group_id' => array(
                            'type'          => 'static',
                            'input'         => 'select',
                            'label'         => 'Group',
                            'source'        => 'customer/customer_attribute_source_group',
                            'sort_order'    => 70,
                        ),
                        'dob' => array(
                            'type'          => 'datetime',
                            'input'         => 'date',
                            'backend'       => 'eav/entity_attribute_backend_datetime',
                            'required'      => false,
                            'label'         => 'Date Of Birth',
                            'sort_order'    => 80,
                        ),
                        'password_hash' => array(
                            'input'         => 'hidden',
                            'backend'       => 'customer/customer_attribute_backend_password',
                            'required'      => false,
                        ),
                        'default_billing' => array(
                            'type'          => 'int',
                            'visible'       => false,
                            'required'      => false,
                            'backend'       => 'customer/customer_attribute_backend_billing',
                        ),
                        'default_shipping' => array(
                            'type'          => 'int',
                            'visible'       => false,
                            'required'      => false,
                            'backend'       => 'customer/customer_attribute_backend_shipping',
                        ),
                        'taxvat' => array(
                            'label'         => 'Tax/VAT Number',
                            'visible'       => true,
                            'required'      => false,
                        ),
                        'confirmation' => array(
                            'label'         => 'Is Confirmed',
                            'visible'       => false,
                            'required'      => false,
                        ),
                        'created_at' => array(
                            'type'          => 'static',
                            'label'         => 'Created At',
                            'visible'       => false,
                            'required'      => false,
                            'input'         => 'date',
                        ),
                    ),
                ),
    
                'customer_address'=>array(
                    'entity_model'  =>'customer/customer_address',
                    'table' => 'customer/address_entity',
                    'additional_attribute_table' => 'customer/eav_attribute',
                    'entity_attribute_collection' => 'customer/address_attribute_collection',
                    'attributes' => array(
    //                    'entity_id'         => array('type'=>'static'),
    //                    'entity_type_id'    => array('type'=>'static'),
    //                    'attribute_set_id'  => array('type'=>'static'),
    //                    'increment_id'      => array('type'=>'static'),
    //                    'parent_id'         => array('type'=>'static'),
    //                    'created_at'        => array('type'=>'static'),
    //                    'updated_at'        => array('type'=>'static'),
    //                    'is_active'         => array('type'=>'static'),
    
                        'prefix' => array(
                            'label'         => 'Prefix',
                            'required'      => false,
                            'sort_order'    => 7,
                        ),
                        'firstname' => array(
                            'label'         => 'First Name',
                            'sort_order'    => 10,
                        ),
                        'middlename' => array(
                            'label'         => 'Middle Name/Initial',
                            'required'      => false,
                            'sort_order'    => 13,
                        ),
                        'lastname' => array(
                            'label'         => 'Last Name',
                            'sort_order'    => 20,
                        ),
                        'suffix' => array(
                            'label'         => 'Suffix',
                            'required'      => false,
                            'sort_order'    => 23,
                        ),
                        'company' => array(
                            'label'         => 'Company',
                            'required'      => false,
                            'sort_order'    => 30,
                        ),
                        'street' => array(
                            'type'          => 'text',
                            'backend'       => 'customer_entity/address_attribute_backend_street',
                            'input'         => 'multiline',
                            'label'         => 'Street Address',
                            'sort_order'    => 40,
                        ),
                        'city' => array(
                            'label'         => 'City',
                            'sort_order'    => 50,
                        ),
                        'country_id' => array(
                            'type'          => 'varchar',
                            'input'         => 'select',
                            'label'         => 'Country',
                            'class'         => 'countries',
                            'source'        => 'customer_entity/address_attribute_source_country',
                            'sort_order'    => 60,
                        ),
                        'region' => array(
                            'backend'       => 'customer_entity/address_attribute_backend_region',
                            'label'         => 'State/Province',
                            'class'         => 'regions',
                            'sort_order'    => 70,
                        ),
                        'region_id' => array(
                            'type'          => 'int',
                            'input'         => 'hidden',
                            'source'        => 'customer_entity/address_attribute_source_region',
                            'required'      => 'false',
                            'sort_order'    => 80,
                            'label'         => 'State/Province'
                        ),
                        'postcode' => array(
                            'label'         => 'Zip/Postal Code',
                            'sort_order'    => 90,
                        ),
                        'telephone' => array(
                            'label'         => 'Telephone',
                          开发者_运维问答  'sort_order'    => 100,
                        ),
                        'fax' => array(
                            'label'         => 'Fax',
                            'required'      => false,
                            'sort_order'    => 110,
                        ),
                    ),
                ),
            );
        }   
}

app/design/frontend/default/company/template/customer/form/register.phtml

<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label>
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getFormData()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" />

i also run this piece of php code in the top of register.phtml to add the attribute to the eav_attribute this code was executed once:

<?php
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $AttrCode = 'nickname';
    $settings = array (
        'position' => 1,
        'is_required' => 0
    );
    $setup->addAttribute('1', $AttrCode, $settings);
?>

app/design/frontend/default/company/template/customer/form/edit.phtml

<label for="nickname" class="required" style="margin-left: -1px;"><em>*</em><?php echo $this->__('Nickname') ?></label>
<input type="text" name="nickname" id="nickname" value="<?php echo $this->htmlEscape($this->getCustomer()->getnickname()) ?>" title="<?php echo $this->__('Nickname') ?>" class="input-text required-entry" />

I also manually cleared the /var/cache folder but without any effect.

I have no idea what else I could do, so any help is very welcome.

EDIT:

Ok here is what i have tried on the magento root:

define('MAGENTO', realpath(dirname(__FILE__)));
ini_set('memory_limit', '32M');
set_time_limit (0);
require_once MAGENTO . '/app/Mage.php';

Mage::app();

$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);

$setup->addAttribute('customer', 'nickname', array(
    'input'         => 'text',
    'type'          => 'text',
    'label'         => 'Nickname',
    'visible'       => 1,
    'required'      => 1,
    'user_defined'  => 1,
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'nickname',
 '999'  //sort_order
);


$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'nickname');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));

$oAttribute->save();
$installer->endSetup();

This code will be executed fine and there is no error message or something but also no new attribute added to the eav_attribute table.


I edited your script and it worked fine for me, I can see new attribute in backed now.

Replace this:

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

With this:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜