Customise the OpenCart registration form with this simple guide which shows you how to add new fields to capture new information from your customers signing up

来源: How To Add Custom Fields To The OpenCart Registration Form – Cart Advisor

I’ve seen a lot of people requesting this one so finally, here it is. In this blog post we will be looking at how to add your own fields to the OpenCart registration page to add things like Gender, Birthday and Age to the customer account. Through this blog post we will be adding a Website text input to the registration form and creating a place to store it in the database.

So, to do this we need to take four steps:

  1. Create a place to store the new information in the database
  2. Add the dropdown to the template file
  3. Add our new data input to the SQL query which stores the info

It sounds a bit laborious when it’s laid out like that but it’s actually not that difficult to do and we’ll go through it step by step so you can add more later on if you want by following the same process. So, lets get started!

 Adding the new info space to the OpenCart database

The first step we need to take is to add a new column to the “oc_customer” database table which will store the new info we will be capturing on the registration form. To do this, open up your OpenCart database in PHPMyAdmin (or whatever you use) and load up the oc_customer database table. You should see something like:

phpmyadmin

Just add a new column after the “password” column and name it “website”, VARCHAR with  character limit of 100. As in the screenshot below:

phpmyadmin3

 

Click save, and that’s it. We now have a place in the database to save the gender of our new OpenCart customers.

Adding the custom field to the OpenCart registration form

Now, lets actually create the dropdown in the registration form so that customers have a way of selecting which gender they are. To do this, open up:

  • catalog/view/theme/[YOUR THEME]/template/account/register.tpl

I will just add a very simple dropdown with the code below (in this example I have added the dropdown after the Fax input on the registration page):

1
2
3
4
5
6
7
8
9
<tr>
<td><?php echo $entry_fax ; ?></td>
<td><input type= "text" name= "fax" value= "<?php echo $fax; ?>" /></td>
</tr></pre>
<pre><tr>
<td>Website: </td>
<td><input type= "text" name= "website" value= "<?php echo $website; ?>" /></td>
</tr></pre>
<pre>

Now save and upload the register.tpl file back to where it was.

As you can see, we now have a new text box which asks customers to enter their website URL as well. Don’t worry about that error which shows up, the next section will get rid of that.

website1

Next, we will add something to the controller which processes the info.

Processing the custom registration field in the OpenCart Controller

OK, now we just need to tell OpenCart what this new field is and what it should be set to when the page is being loaded for the first time or posted. To do this, open up the following file:

  • catalog/controller/account/register.php

Find the following code:

1
2
3
4
5
if (isset( $this ->request->post[ 'fax' ])) {
$this ->data[ 'fax' ] = $this ->request->post[ 'fax' ];
} else {
$this ->data[ 'fax' ] = '' ;
}

And then add a new section which is going to define our new “website” value in the form. Your code should now look like this:

1
2
3
4
5
6
7
8
9
10
11
if (isset( $this ->request->post[ 'fax' ])) {
$this ->data[ 'fax' ] = $this ->request->post[ 'fax' ];
} else {
$this ->data[ 'fax' ] = '' ;
}
if (isset( $this ->request->post[ 'website' ])) {
$this ->data[ 'website' ] = $this ->request->post[ 'website' ];
} else {
$this ->data[ 'website' ] = '' ;
}

Save and upload this file back and you can see that the error has disappeared from the Registration form now. We’re nearly finished now, we just need to actually save the website field to the database and we’re done.

Adding the Website field to the OpenCart SQL query

This sounds scarier than it actually is. Just open up the following file:

  • catalog/model/account/customer.php

The very first function in the file should be the addCustomer() one which is the one we need to edit slightly. If you find the actual SQL query section, which looks like:

1
$this ->db->query( "INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int) $this ->config->get('config_store_id ') . "' , firstname = '" . $this->db->escape($data[' firstname ']) . "' , lastname = '" . $this->db->escape($data[' lastname ']) . "' , email = '" . $this->db->escape($data[' email ']) . "' , telephone = '" . $this->db->escape($data[' telephone ']) . "' , fax = '" . $this->db->escape($data[' fax ']) . "' , salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "' , password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data[' password '])))) . "' , newsletter = '" . (isset($data[' newsletter ']) ? (int)$data[' newsletter '] : 0) . "' , customer_group_id = '" . (int)$customer_group_id . "' , ip = '" . $this->db->escape($this->request->server[' REMOTE_ADDR ']) . "' , status = '1' , approved = '" . (int)!$customer_group_info[' approval '] . "' , date_added = NOW()");

Now just add a new section after the fax section which saves our (secured) website input from the registration form. The code should now look like this:

1
$this ->db->query( "INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int) $this ->config->get('config_store_id ') . "' , firstname = '" . $this->db->escape($data[' firstname ']) . "' , lastname = '" . $this->db->escape($data[' lastname ']) . "' , email = '" . $this->db->escape($data[' email ']) . "' , telephone = '" . $this->db->escape($data[' telephone ']) . "' , fax = '" . $this->db->escape($data[' fax ']) . "' , website = '" . $this->db->escape($data[' website ']) . "' , salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "' , password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data[' password '])))) . "' , newsletter = '" . (isset($data[' newsletter ']) ? (int)$data[' newsletter '] : 0) . "' , customer_group_id = '" . (int)$customer_group_id . "' , ip = '" . $this->db->escape($this->request->server[' REMOTE_ADDR ']) . "' , status = '1' , approved = '" . (int)!$customer_group_info[' approval '] . "' , date_added = NOW()");

And, that’s it! You have added a custom field to your OpenCart registration form and saved it to the database. You can repeat this process to add more and more elements to be captured at the point of registration as your business requires it. Let me know if you found this useful, or have any questions,  in the comments below.

UPDATE

You can use the same process to add new fields to the other registration fields throughout OpenCart (there are multiple places a user can register an account!) but I won’t be able to guide you from each of them otherwise this post would end up far too long! If it gets asked for though, I will consider writing guides for every registration form.

Also remember, with anything involving editing core files, that this may get overwritten when updating OpenCart so be prepared to follow the process again once you have upgraded or use this tutorial to build a vqMod extensionwhich will keep it safe from updates.

CATEGORIES: OPENCART
3 Responses to How To Add Custom Fields To The OpenCart Registration Form – Cart Advisor

[top]

Leave a Reply

Your email address will not be published. Required fields are marked *