magento 定制注册字段并添加到数据库

This item was filled under [ magento ]

Alright, we will be dealing with 3 files. So open each of the following up:

app/design/frontend/default/yourstore/template/customer/form/register.html – Has the HTML form
app/code/core/Mage/Customer/Model/Entity/Setup.php – Has an array full of customer attributes to use for registration
app/code/core/Mage/Customer/controllers/AccountController.php – Has a specific block of code for registration

Alright, for this little tutorial, we will do a couple of fields. The first one will be a text box asking the customer for their occupation/title.
Navigate to somewhere around line 54 of register.html – You should see this block of code:

<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>

Now, we need to add our code for the occupation text box, so change that block of code to the following:

<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>

Ok, if you refresh your create account (register) page, you will see the new field show up. Now, the fun part is getting that value into the database. Now, navigate to around line 78 in Setup.php – you should see this block of code:

'email' => array(
'type' => 'static',
'label' => 'Email',
'class' => 'validate-email',
'sort_order' => 6,
),

Now, we need to add our attribute to this file. So, change this block of code to:

'email' => array(
'type' => 'static',
'label' => 'Email',
'class' => 'validate-email',
'sort_order' => 6,
),
'occupation' => array(
'label' => 'Occupation',
'required' => false,
'sort_order' => 7,
),

Right under your occupation array, you will see the group_id one, etc. You need to increment the sort_order as they were doing before. So, since the sort_order for occupation is 7, make the one after it 8, and so on.

Now, head to line 164 of AccountController.php. You should see this block of code:

$customer = Mage::getModel('customer/customer')
->
setFirstname($this->getRequest()->getPost('firstname'))
->
setLastname($this->getRequest()->getPost('lastname'))
->
setEmail($this->getRequest()->getPost('email'))
->
setPassword($this->getRequest()->getPost('password'))
->
setConfirmation($this->getRequest()->getPost('confirmation'))
->
setId(null);

We need to add our new occupation attribute to this, so change the code to this:

$customer = Mage::getModel('customer/customer')
->
setFirstname($this->getRequest()->getPost('firstname'))
->
setLastname($this->getRequest()->getPost('lastname'))
->
setEmail($this->getRequest()->getPost('email'))
->
setOccupation($this->getRequest()->getPost('occupation')) //Add this
->setPassword($this->getRequest()->getPost('password'))
->
setConfirmation($this->getRequest()->getPost('confirmation'))
->
setId(null);

Now, our code is all set up and ready. However, we still need to add this attribute to the eav_attribute table in your mysql database. How do we do that? This block of code:

<?
$setup
= new Mage_Eav_Model_Entity_Setup('core_setup');
$attr2 = array (
'position' => 1,
'is_required'=>0 /* 0 if you want it to be a required field, 1 if not. Also, if required, add this class to the occupation input box on register.phtml class="required-entry input-text" */
);
$setup->addAttribute('1', 'occupation', $attr2);
?>

My suggestion is to add this block of code to the very top of your register.html file. You only need to load this block of code once, so add it to your register.html file, navigate to your register.html file, and then remove the block of code. If you go to your mysql database, view the eav_attribute table and do a search for occupation, you will see that your attribute has been added.

Now, go back to your create account page, fill it out, including your occupation, and bam your finished. If you need proof that it was added to the database, simply go to the customer_entity_varchar table and look for yourself smile

Also, to retrieve the customer data (as if you were in the account dashboard or something) you can call this function: getOccupation. I believe this works – if not, then hopefully someone else can provide this part of the code.

Thanks all!

Bookmark and Share

喜欢这篇文章的人还喜欢。。。

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Comment