magento1给customer添加自定义属性

在模块的sql文件中:
<?php
/**

  • Add Attribute mobile for customer
    */

//echo 12345;
$installer = $this;
$installer->startSetup();
$eavConfig = Mage::getSingleton('eav/config');
$store  = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$sortOrder = 999;
$attributes = array(
'chuanzhen'              => array(
    'type'          => 'varchar',
    'label'         => 'chuanzhen',
    'input'         => 'text',
    'required'      => 1,
    'global'        => 1,
    'is_visible'    => 1,
    'is_system'     => 0,
    'position'      => 40,
    'is_user_defined'  => 1,
    'sort_order'    => $sortOrder++,
),
'sex'                   => array(
    'type'          => 'int',
    'label'         => 'Sex',
    'input'         => 'radio',
    'required'      => 1,
    'is_visible'    => 1,
    'is_system'     => 0,
    'global'        => 1,
    'is_user_defined'  => 1,
    'position'      => 44,
    'sort_order'    => $sortOrder++,
),

);
/将以上属性存入数据库,并且添加到页面表单中/

foreach ( $attributes as $attributeCode => $data ){
    $installer->addAttribute('customer',$attributeCode,$data);
}

foreach ($attributes as $attributeCode => $data) {
    $attribute = $eavConfig->getAttribute('customer', $attributeCode);
    $attribute->setWebsite( $store->getWebsite () );
    $attribute->addData( $data );
    if (false === ($data['is_system'] == 1 && $data['is_visible'] == 0)) {
        $usedInForms = array(
            'customer_account_create',
            'customer_account_edit',
            'checkout_register',
            'adminhtml_customer',
            'adminhtml_checkout'
        );
        $attribute->setData('used_in_forms', $usedInForms);
    }
    $attribute->save();
}
$installer->endSeup();



添加下拉框的属性,添加性别 male 和 famale:
$installer->startSetup();  
  
$installer->addAttribute('customer', 'gender', array(  
    'label'        => 'Gender',  
    'visible'      => true,  
    'required'     => false,  
    'type'         => 'int',  
    'input'        => 'select',  
    'source'        => 'eav/entity_attribute_source_table',  
));  
$tableOptions        = $installer->getTable('eav_attribute_option');  
$tableOptionValues   = $installer->getTable('eav_attribute_option_value');  
  
// add options for level of politeness  
$attributeId = (int)$installer->getAttribute('customer', 'gender', 'attribute_id');  
foreach (array('Male', 'Female') as $sortOrder => $label) {  
  
// add option  
$data = array(
    'attribute_id' => $attributeId,  
    'sort_order'   => $sortOrder,  
);  
$installer->getConnection()->insert($tableOptions, $data);  

// add option label  
$optionId = (int)$installer->getConnection()->lastInsertId($tableOptions, 'option_id');  
$data = array(  
    'option_id' => $optionId,  
    'store_id'  => 0,  
    'value'     => $label,  
);  
$installer->getConnection()->insert($tableOptionValues, $data);  

}

$installer->endSetup();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值