Magento - 向客户实体添加属性
Posted
技术标签:
【中文标题】Magento - 向客户实体添加属性【英文标题】:Magento - Add attribute to customer entity 【发布时间】:2011-07-16 12:07:28 【问题描述】:这两天我一直在尝试添加新的客户属性到 magento 数据库。但它不起作用。按照本教程,我在register.phtml
和edit.phtml
中添加了一个简单的nickname
输入字段:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields
这是固有的论坛帖子:
http://www.magentocommerce.com/boards/viewthread/9620/
属性已添加到数据库表eav_attribute
,但值未出现在customer_entity_varchar
中。
这是我所拥有的:
所有文件都位于app/code/local/Company/
下
app/code/local/Company/Customer/etc/config.xml
(注意:<!-- 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 prefixvar prefix /dependvar firstname depend middlenamevar middlename /dependvar lastnamedepend suffix var suffix/depend
depend companyvar company/depend
var street1
depend street2var street2/depend
depend street3var street3/depend
depend street4var street4/depend
depend cityvar city, /dependdepend regionvar region, /dependvar postcode
var country
T: var telephone
depend faxF: var fax/depend
depend mobileM: var mobile/depend
]]></defaultFormat>
</text>
<oneline translate="title" module="customer">
<title>Text One Line</title>
<htmlEscape>true</htmlEscape>
<defaultFormat>
<![CDATA[depend prefixvar prefix /dependvar firstname depend middlenamevar middlename /dependvar lastnamedepend 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 prefixvar prefix /dependvar firstname depend middlenamevar middlename /dependvar lastnamedepend suffix var suffix/depend<br/>
depend companyvar company<br />/depend
var street1<br />
depend street2var street2<br />/depend
depend street3var street3<br />/depend
depend street4var street4<br />/depend
depend cityvar city, /dependdepend regionvar region, /dependvar postcode<br/>
var country<br/>
depend telephoneT: 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 prefixvar prefix /dependvar firstname depend middlenamevar middlename /dependvar lastnamedepend suffix var suffix/depend|
depend companyvar company|/depend
var street1|
depend street2var street2|/depend
depend street3var street3|/depend
depend street4var street4|/depend
depend cityvar city, /dependdepend regionvar region, /dependvar postcode|
var country|
depend telephoneT: 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
(再次注意:/* 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" />
我还在 register.phtml 的顶部运行这段 php 代码,以将属性添加到 eav_attribute 这段代码执行了一次:
<?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" />
我也手动清除了 /var/cache 文件夹,但没有任何效果。
我不知道我还能做什么,所以非常欢迎任何帮助。
编辑:
好的,这是我在 magento 根上尝试过的:
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();
这段代码将被正常执行,没有错误消息或其他东西,但也没有向 eav_attribute 表添加新属性。
【问题讨论】:
随着时间的推移,您粘贴的链接将过期,其他人将无法使用您的帖子解决他们自己的问题。请编辑您的帖子并在帖子中内联相关的代码部分,就像您对设置文件所做的那样。 @joseph-mastey 你是对的,但我尝试添加整个代码,但它已被剪切且不再完全,这就是我使用 mysticpaste 的原因。但是,我会再试一次。 @joseph-mastey 我添加了上面的Setup.php,但是xml-code的转换不能正常工作,会省略后半部分的标签。 Adding attributes to customer entity 的可能副本 您的问题解决了吗?如果是的话,我想知道怎么做,因为我遇到了类似的情况。谢谢 【参考方案1】:我编辑了你的脚本,它对我来说很好,我现在可以看到支持的新属性。
替换这个:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
有了这个:
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();
【讨论】:
以上是关于Magento - 向客户实体添加属性的主要内容,如果未能解决你的问题,请参考以下文章
PayPal 在 PayPal 方面向 Magento 购物车添加额外的税收
NHibernate - 向实体添加评论属性(使用加入存储)
magento麦进斗客户地址属性不保存在sales_flat_order_address
为啥向核心数据实体添加双重属性会导致远距离相关实体中的属性冲突出现 NSInternalInconsistencyException?