博客
关于我
magento1给customer添加自定义属性
阅读量:799 次
发布时间:2023-02-06

本文共 2833 字,大约阅读时间需要 9 分钟。

在模块的SQL文件中,添加了以下功能:首先,新增了“chuanzhen”和“sex”属性,分别对应身份证号和性别字段。其次,通过循环添加这些属性到页面表单中,确保数据准确同步到数据库。最后,手动添加性别下拉框选项,支持“Male”和“Female”两个值。

// 初始化设置
$installer = $this;
$installer->startSetup();
// 获取当前店铺信息
$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 ($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->endSetup();
// 添加性别下拉框选项
$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');
$attributeId = (int)$installer->getAttribute('customer', 'gender', 'attribute_id');
foreach (array('Male', 'Female') as $sortOrder => $label) {
$installer->getConnection()->insert($tableOptions, array(
'attribute_id' => $attributeId,
'sort_order' => $sortOrder
));
$optionId = (int)$installer->getConnection()->lastInsertId($tableOptions, 'option_id');
$installer->getConnection()->insert($tableOptionValues, array(
'option_id' => $optionId,
'store_id' => 0,
'value' => $label
));
}
$installer->endSetup();

转载地址:http://ahufk.baihongyu.com/

你可能感兴趣的文章
Nginx的使用总结(一)
查看>>
Nginx的使用总结(三)
查看>>
Nginx的使用总结(二)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>
nginx负载均衡和反相代理的配置
查看>>
nginx负载均衡器处理session共享的几种方法(转)
查看>>
nginx负载均衡的5种策略(转载)
查看>>
nginx负载均衡的五种算法
查看>>
nginx转发端口时与导致websocket不生效
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx配置Https证书
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
nginx配置全解
查看>>
Nginx配置参数中文说明
查看>>