[Magento]用户信息添加自定义字段

要将以前网站的用户数据导入到目前magento项目中,有很多magento没有的字段需要我们手动添加,

参考官方介绍:http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields

( 要实现官网介绍中前端注册时也有相关字段,还需其它操作)

只看数据表添加部份,添加数据重复参数比较多,

比较快的方法,你可以直接创建一个php文件,运行一次即可,

require_once MAGENTO . '/../app/Mage.php';
Mage::app();

/*也可以创建新文件,不写上面两句,直接将下面的代码放到注册页面文件中(app/design/frontend/default/template/persistent/customer/form/register.phtml),然后到前台运行注册页面一次即可.*/ 
$newFields = array(
    'custom_attribute1' => array(
        'type'              => 'text',
        'label'                => 'Customer Custom Attribute1'
    )
    'custom_attribute2' => array(
        'type'              => 'text',
        'label'                => 'Customer Custom Attribute2'
    )
);

$entities = array('customer');

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
foreach($newFields as $attributeName => $attributeDefs) {
    foreach ($entities as $entity) {
        $setup->addAttribute($entity, $attributeName, array(
            'position'          => 1,
            'type'              => $attributeDefs['type'],
            'label'             => $attributeDefs['label'],
            'global'            => 1,
            'visible'           => 1,
            'required'          => 0,
            'user_defined'      => 1,
            'searchable'        => 0,
            'filterable'        => 0,
            'comparable'        => 0,
            'visible_on_front'  => 1,
            'visible_in_advanced_search' => 0,
            'unique'            => 0,
            'is_configurable'   => 0,
            'position'          => 1,
        ));                
    }
}

这样就只需要将拥有不同参数的数据单独写即可.其它参数通过循环写入

同样是register.phtml文件,单条数据也可以这样添加:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$AttrCode = 'Custom_Attribute';
$settings = array (
    'position' => 1,
    'label' => 'Customer Custom Attribute'
    'is_required'=> 0
);
$setup->addAttribute('1', $AttrCode, $settings);

网上还有另一段代码:(此代码我没试过,只作记录)

$installer = $this;

$installer->startSetup();

$installer->addAttribute('customer', 'interested',array(
	'type'      => 'varchar',
	'input'		=> 'text',
	'visible'   => true,
	'label'     => 'Interested sales',
 	'global'    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
	'required'	=> false
)); 

$installer->endSetup();

 

686 total views, 2 views today

Revisions

There are no revisions for this post.

No comments yet.

发表评论