一直在这个问题上敲了8个小时...
我需要(或者更确切地说,我的公司确实是 B2B)在客户注册时请求额外的属性。当然,Magento 本身并没有这样做,所以我们被迫沿着极其复杂的构建路径来实现它。我买了一个模块来做这件事,但发现代码很乱(更不用说根本不工作)。
任何。我想我会自己动手。
所以我找到了一个看似清晰且切中要害的教程。所以我,花几分钟把它放在一起。没事了。
进一步的 google-fu 将我带到这里和这里以获得额外的热智慧酱。
这是我所拥有的(运行 Magento Professional v1.11 ):
文件结构:
WACI
- Customer
-- etc
--- config.xml
-- Model
--- Resource
---- Eav
----- Mysql4
------ Setup.php
-- sql
--- customer_setup
---- mysql4-install-0.1.0.php
等/模块/WACI_All.xml
<config>
<modules>
<WACI_Customer>
<active>true</active>
<codePool>local</codePool>
</WACI_Customer>
</modules>
</config>
配置文件
<config>
<modules>
<WACI_Customer>
<version>0.1.0</version>
</WACI_Customer>
</modules>
<global>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
<!--<models>
<customer>
<class>WACI_Customer_Model</class>
</customer>
</models> -->
<resources>
<customer_setup>
<setup>
<module>WACI_Customer</module>
<class>WACI_Customer_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customer_setup>
<customer_write>
<connection>
<use>core_write</use>
</connection>
</customer_write>
<customer_read>
<connection>
<use>core_read</use>
</connection>
</customer_read>
</resources>
</global>
</config>
安装程序.php
<?php
class WACI_Customer_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_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,
'attribute_model' => 'customer/attribute',
'attributes' => array(
'title' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Title / Position',
'visible' => true,
'required' => false,
'position' => 63,
),
'phone' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Telephone',
'visible' => true,
'required' => true,
'position' => 64,
),
'agency' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Agency / Organization',
'visible' => true,
'required' => false,
'position' => 65,
),
'fed_id' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Fed ID',
'visible' => true,
'required' => false,
'position' => 66,
),
'ubi' => array(
'type' => 'varchar',
'input' => 'text',
'label' => 'UBI',
'visible' => true,
'required' => false,
'position' => 67,
),
),
),
);
}
}
?>
mysql4-install-0.1.0.php
<?php
Mage::log('Installing WACI_Customer');
// die ( echo 'Running This Upgrade: '.get_class($this)."\n <br /> \n"; );
$installer = $this;
$installer->installEntities();
$eavConfig = Mage::getSingleton(‘eav/config’);
$attribute_title = $eavConfig->getAttribute(‘customer’, 'title');
$attribute_phone = $eavConfig->getAttribute(‘customer’, 'phone');
$attribute_agency = $eavConfig->getAttribute(‘customer’, 'agency');
$attribute_fedid = $eavConfig->getAttribute(‘customer’, 'fed_id');
$attribute_ubi = $eavConfig->getAttribute(‘customer’, 'ubi');
// put into customer_form_attribute table so field will show in admin.
$attribute_title->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_phone->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_agency->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_fedid->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$attribute_ubi->setData(‘used_in_forms’, array(‘adminhtml_customer’));
?>
假设我理解正确,我已经准备好了一切......但是什么都没有,什么都没有,甚至什么都没有。
尤其:
- 如果我删除安装类,我会收到一个致命错误,所以我认为一切都准备好了。
- 该模块显示在 admin/system/configuration/advanced/(已启用)中;它被写入
core_config_data
- 我删除了所有代码,只使用了空容器(带有调试内容)——安装脚本永远不会运行;它永远不会被写入
core_resource
问题,然后是两个部分:
1. 是什么阻止了我的安装脚本运行?
2. 创建新客户属性的逻辑是否合理?
假设是 - 我认为将它放到注册页面/客户帐户/管理员帐户应该相当简单......
......现在要去喝啤酒了。
干杯。
更新
正如@AlexeiYerofeyev 所想,customer
名称本身就是问题所在。更改为 CustomerAttr,脚本立即运行。
然后该模块按预期运行,但@benmarks 的解决方案似乎更干净,所以我重写以匹配:
配置.xml 0.1.0
<global>
<resources>
<customerattr_setup>
<setup>
<module>WACI_CustomerAttr</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customerattr_setup>
</resources>
<fieldsets>
<customer_account>
<title><create>1</create><update>1</update></title>
<phone><create>1</create><update>1</update></phone>
<agency><create>1</create><update>1</update></agency>
<fed_id><create>1</create><update>1</update></fed_id>
<ubi><create>1</create><update>1</update></ubi>
</customer_account>
</fieldsets>
</global>
</config>
安装程序.php
<?php
Mage::log('Installing WACI_CustomerAttr');
echo 'Running Upgrade: '.get_class($this)."\n <br /> \n";
//die ( 'its running' );
$installer = $this;
/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();
$installer->addAttribute('customer','agency',
array(
'type' => 'varchar',
'label' => 'Agency / Organization',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 62,
)
);
$installer->addAttribute('customer','title',
array(
'type' => 'varchar',
'label' => 'Title / Position',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 63,
)
);
$installer->addAttribute('customer','phone',
array(
'type' => 'varchar',
'label' => 'Telephone',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 64,
)
);
$installer->addAttribute('customer','fed_id',
array(
'type' => 'varchar',
'label' => 'Fed ID',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 65,
)
);
$installer->addAttribute('customer','ubi',
array(
'type' => 'varchar',
'label' => 'UBI',
'input' => 'text',
'required' => false,
'visible' => true,
'position' => 66,
)
);
$attrs = array('agency','title','phone','fed_id','ubi');
foreach ($attrs as $item) {
$attr = Mage::getSingleton('eav/config')->getAttribute('customer', $item);
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
}
$installer->endSetup();
?>
模块(和版本)成功写入core_resource
,并且属性成功添加到eav_attribute
。
我可以打电话给现场
主题/模板/客户/表单/edit.phtml
<div class="input-box">
<label for="agency"><?php echo $this->__('Agency / Organization') ?><span class="required">*</span></label><br />
<input type="text" name="agency" id="agency" value="<?php echo $this->htmlEscape($this->getCustomer()->getAgency()) ?>" title="<?php echo $this->__('Agency') ?>" class="required-entry input-text" />
</div>
但是,不幸的是,该值没有被写入customer_entity_varchar
(最终,我的初始脚本也是如此。
所以,我在表中得到了属性,但它还没有被添加到客户实体中。
似乎一旦我开始工作,我应该能够在任何需要的地方随意读取和写入该值。
关于我现在如何进行的任何想法?
最终更新
好的,解决了这个问题:
$attr->setData('used_in_forms', array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
代替
$attr->setIsUsedInForms(array('adminhtml_customer','customer_account_edit','customer_account_create'))->save();
对于任何可能需要信息的人...
原来数组没有customer_form_attribute
使用先前的代码在表中更新。