在Magento的注册流程中增加职业字段

要在magento的注册页面添加字段,需要修改4个文件。

app/design/frontend/default/yourtheme/template/customer/form/register.phtmlHTML 表单

app/design/frontend/default/yourtheme/template/customer/form/edit.phtmlHTML form 用户编辑表单

app/code/core/Mage/Customer/Model/Entity/Setup.php - 客户注册属性的数组

app/code/core/Mage/Customer/etc/config.xml - 表单中各项的描述

首先我们来添加一个文本框用来输入用户的职业occupation/title. 在register.html的第54行 :

  1. <li>
  2.     <div class="input-box">
  3.         <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
  4.         <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" />
  5.     </div>
  6. </li>

现在我们需要添加:职业“(occupation)的文本框, 所以把代码改为:

  1. <li>
  2.     <div class="input-box">
  3.         <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
  4.         <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" />
  5.     </div>
  6.     <div class="input-box">
  7.         <label for="occupation"><?php echo $this->__('Occupation/Title') ?></label><br/>
  8.         <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
  9.     </div>
  10. </li>

现在刷新创建新用户/注册页面,就可以看到新的字段出现了。

用类似的方法修改 edit.phtml, 用 getCustomer替换 getFormData . 在1.1.3 版里, 修改第32行. 旧代码如下:

  1.         <li>
  2.             <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
  3.         </li>
  4.         <li>
  5.             <div class="input-box">
  6.             <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
  7.             <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" />
  8.             </div>
  9.         </li>

用来替换的新代码:

  1.         <li>
  2.             <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
  3.         </li>
  4.         <li>
  5.             <div class="input-box">
  6.             <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
  7.             <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" />
  8.             </div>
  9.         </li>
  10.             <li>
  11.                 <div class="input-box">
  12.                     <label for="occupation"><?php echo $this->__('Occupation') ?> </label><br/>
  13.                     <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
  14.                 </div>
  15.             </li>

现在要把数据插入数据库. 找到 Setup.php的第93行 , 可以看到如下的代码:

  1. 'email' => array(
  2.                         'type'          => 'static',
  3.                         'label'         => 'Email',
  4.                         'class'         => 'validate-email',
  5.                         'sort_order'    => 6,
  6.                     ),

我们需要添加我们自己的属性到这个文件里,所以修改代码为:

  1. 'email' => array(
  2.                         'type'          => 'static',
  3.                         'label'         => 'Email',
  4.                         'class'         => 'validate-email',
  5.                         'sort_order'    => 6,
  6.                     ),
  7. 'occupation' => array(
  8.                         'label'         => 'Occupation',
  9.                         'required'      => false,
  10.                         'sort_order'    => 7,
  11.                     ),

就在职业occupation数组下面, 你会看到group_id 等等. 你需要增加排列顺序(sort_order). 因为职业(occupation )的 sort_order是 7, 所以从8开始类推.

现在我们的代码已经建好了,但是我们仍然需要吧这个属性添加到mysql数据库的 eav_attribute表里. 下面这段代码介绍如何实现:

  1. <?php
  2. $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
  3. $AttrCode = 'ocupation';
  4. $settings = array (
  5.     'position' => 1,
  6.     'is_required'=> 0
  7. );
  8. $setup->addAttribute('1', $AttrCode, $settings);
  9. ?>

我建议这段代码放到 register.html 文件的最上面. 你只需要加载一次这段代码,然后找到并且删除它。如果你到数据库里查看eav_attribute 表,你会发现那个属性已经添加了。

此外,你可以调用函数getOccupation来显示customer数据

原文作者: Chris Woodard

[更新 30 Aug 2008 - AlexSz] 要插入这段数据必须编辑一个 .xml file: app/code/core/Mage/Customer/etc/config.xml 找到 <fieldsets>. 添加下面这行:

  1. <occupation><create>1</create><update>1</update></occupation>

代码摘录如下:

  1. <fieldsets>
  2.             <customer_account>
  3.                 <prefix><create>1</create><update>1</update><name>1</name></prefix>
  4.                 <firstname><create>1</create><update>1</update><name>1</name></firstname>
  5.                 <middlename><create>1</create><update>1</update><name>1</name></middlename>
  6.                 <lastname><create>1</create><update>1</update><name>1</name></lastname>
  7.                 <suffix><create>1</create><update>1</update><name>1</name></suffix>
  8.                 <email><create>1</create><update>1</update></email>
  9.                 <password><create>1</create></password>
  10.                 <confirmation><create>1</create></confirmation>
  11.                 <dob><create>1</create><update>1</update></dob>
  12.                 <taxvat><create>1</create><update>1</update></taxvat>
  13.                 <occupation><create>1</create><update>1</update></occupation>
  14.             </customer_account>
  15.         </fieldsets>

[修改 - AlexSz]