[转]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();
From: https://segmentfault.com/a/1190000005008696