要在magento的注册页面添加字段,需要修改4个文件。
app/design/frontend/default/yourtheme/template/customer/form/register.phtml - HTML 表单
app/design/frontend/default/yourtheme/template/customer/form/edit.phtml - HTML form 用户编辑表单
app/code/core/Mage/Customer/Model/Entity/Setup.php - 客户注册属性的数组
app/code/core/Mage/Customer/etc/config.xml - 表单中各项的描述
首先我们来添加一个文本框用来输入用户的职业occupation/title. 在register.html的第54行 :
-
<li>
-
<div class="input-box">
-
<label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
-
<input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
-
</div>
-
</li>
现在我们需要添加:职业“(occupation)的文本框, 所以把代码改为:
-
<li>
-
<div class="input-box">
-
<label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
-
<input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
-
</div>
-
<div class="input-box">
-
<label for="occupation"><?php echo $this->__('Occupation/Title') ?></label><br/>
-
<input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
-
</div>
-
</li>
现在刷新创建新用户/注册页面,就可以看到新的字段出现了。
用类似的方法修改 edit.phtml, 用 getCustomer替换 getFormData . 在1.1.3 版里, 修改第32行. 旧代码如下:
-
<li>
-
<?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
-
</li>
-
<li>
-
<div class="input-box">
-
<label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
-
<input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
-
</div>
-
</li>
用来替换的新代码:
-
<li>
-
<?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
-
</li>
-
<li>
-
<div class="input-box">
-
<label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
-
<input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
-
</div>
-
</li>
-
<li>
-
<div class="input-box">
-
<label for="occupation"><?php echo $this->__('Occupation') ?> </label><br/>
-
<input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
-
</div>
-
</li>
现在要把数据插入数据库. 找到 Setup.php的第93行 , 可以看到如下的代码:
-
'email' => array(
-
'type' => 'static',
-
'label' => 'Email',
-
'class' => 'validate-email',
-
'sort_order' => 6,
-
),
我们需要添加我们自己的属性到这个文件里,所以修改代码为:
-
'email' => array(
-
'type' => 'static',
-
'label' => 'Email',
-
'class' => 'validate-email',
-
'sort_order' => 6,
-
),
-
'occupation' => array(
-
'label' => 'Occupation',
-
'required' => false,
-
'sort_order' => 7,
-
),
就在职业occupation数组下面, 你会看到group_id 等等. 你需要增加排列顺序(sort_order). 因为职业(occupation )的 sort_order是 7, 所以从8开始类推.
现在我们的代码已经建好了,但是我们仍然需要吧这个属性添加到mysql数据库的 eav_attribute表里. 下面这段代码介绍如何实现:
-
<?php
-
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
-
$AttrCode = 'ocupation';
-
$settings = array (
-
'position' => 1,
-
'is_required'=> 0
-
);
-
$setup->addAttribute('1', $AttrCode, $settings);
-
?>
我建议这段代码放到 register.html 文件的最上面. 你只需要加载一次这段代码,然后找到并且删除它。如果你到数据库里查看eav_attribute 表,你会发现那个属性已经添加了。
此外,你可以调用函数getOccupation来显示customer数据
原文作者: Chris Woodard
[更新 30 Aug 2008 - AlexSz] 要插入这段数据必须编辑一个 .xml file: app/code/core/Mage/Customer/etc/config.xml 找到 <fieldsets>. 添加下面这行:
-
<occupation><create>1</create><update>1</update></occupation>
代码摘录如下:
-
<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>
-
<occupation><create>1</create><update>1</update></occupation>
-
</customer_account>
-
</fieldsets>
[修改 - AlexSz]