How to Add Custom Fields to Customers in OpenCart 1.5

  • SumoMe

EDIT Nov 5, 2011: This mod was tested on version 1.5.1.2

Today I will show you how to add custom fields to customers in OpenCart 1.5, which is one of the most exciting shopping carts on the internet to date. I will base this guide off this awesome tutorial by Qphoria in one of the old posts in OpenCart’s forums, so I highly suggest that you read it first. In this example let us also add a salutation field for our customers.

Let’s get started:

Step 1: Add column to the database.

Open your favorite MySQL database management tool or phpMyAdmin and look for the customer table. Add a new column for the salutation field, or you can also execute the following SQL script:

ALTER TABLE `customer` ADD `salutation` VARCHAR( 4 )
CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;

Step 2: Add html input tag for the new field in the registration page/view file.

In your OpenCart installation navigate to catalog/view/theme/[your_theme]/template/account and open register.tpl. Whatever changes you make in this file will reflect on your registration page. Find the code that looks like this:

<table class="form">
        <tr>
          <td><span class="required">*</span> <?php echo $entry_firstname; ?></td>
          <td><input type="text" name="firstname" value="<?php echo $firstname; ?>" />
            <?php if ($error_firstname) { ?>
            <span class="error"><?php echo $error_firstname; ?></span>
            <?php } ?></td>
        </tr>

This first row, as you can see, is the input field for First Name. We will insert our Salutation field (which we assume not required) before this, thus becomes:

<table class="form">
	<tr>
          <td><?php echo $entry_salutation; ?></td>
          <td><input type="text" name="salutation" value="<?php echo $salutation; ?>" /></td>
        </tr>
        <tr>
          <td><span class="required">*</span> <?php echo $entry_firstname; ?></td>
          <td><input type="text" name="firstname" value="<?php echo $firstname; ?>" />
            <?php if ($error_firstname) { ?>
            <span class="error"><?php echo $error_firstname; ?></span>
            <?php } ?></td>
        </tr>

Step 3: Define field label in language file.

Navigate to catalog/language/english/account and open register.php. Find the line:

// Entry
$_['entry_firstname']      = 'First Name:';
$_['entry_lastname']       = 'Last Name:';

You will define and insert the definition of $entry_salutation in Step 2 just as the other labels in the form such as $entry_firstname; thus your code becomes:

// Entry
$_['entry_salutation']      = 'Salutation:';
$_['entry_firstname']       = 'First Name:';
$_['entry_lastname']        = 'Last Name:';

Step 4: Introduce new field to the controller file.

Navigate to catalog/controller/account/ and open register.php. Find the line:

$this->data['entry_firstname'] = $this->language->get('entry_firstname');
$this->data['entry_lastname'] = $this->language->get('entry_lastname');
$this->data['entry_email'] = $this->language->get('entry_email');

Here, you will tell OpenCart that the label of the salutation field is defined in the language file (as you have done so in the last step); thus your code becomes:

$this->data['entry_salutation'] = $this->language->get('entry_salutation');
$this->data['entry_firstname'] = $this->language->get('entry_firstname');
$this->data['entry_lastname'] = $this->language->get('entry_lastname');
$this->data['entry_email'] = $this->language->get('entry_email');

Next, find the following line:

if (isset($this->request->post['firstname'])) {
	$this->data['firstname'] = $this->request->post['firstname'];
} else {
	$this->data['firstname'] = '';
}

if (isset($this->request->post['lastname'])) {
	$this->data['lastname'] = $this->request->post['lastname'];
} else {
	$this->data['lastname'] = '';
}

These codes basically retain the value of the field in case the form is not submitted correctly instead of filling up the form all over again. This also defines the variable in your view file, $salutation (in the value attribute of the input tag), as seen the inserted code in Step 2. Your code thus becomes:

if (isset($this->request->post['salutation'])) {
	$this->data['salutation'] = $this->request->post['salutation'];
} else {
	$this->data['salutation'] = '';
}

if (isset($this->request->post['firstname'])) {
	$this->data['firstname'] = $this->request->post['firstname'];
} else {
	$this->data['firstname'] = '';
}

if (isset($this->request->post['lastname'])) {
	$this->data['lastname'] = $this->request->post['lastname'];
} else {
	$this->data['lastname'] = '';
}

Step 5: Include the new field in the SQL INSERT statement found in the model file.

Finally, navigate to catalog/model/account/ and open customer.php. This is where all the database functions happen for the customer table. You just have to find the INSERT statement, which particularly looks like this line:

public function addCustomer($data) {
      	$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']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()");

Add the salutation field that you created in the first step in the INSERT statement so that it looks something like this:

public function addCustomer($data) {
      	$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']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW(), salutation = '" . $this->db->escape($data['salutation']) . "'");

Here the new field is just appended to the INSERT statement. You can see the difference just before the line ends.

And you’re done!

You can test if the whole mod you made is working by creating an account yourself. Fill up the new field you’ve just added and all other required fields and submit it. You can confirm that it is working by checking the newly created record in the customer table using your favorite MySQL database management tool or phpMyAdmin and check if the salutation you entered is correctly inserted. If it does, then congratulations! (If not then check back if you missed something. You can do it ;))

I hope this little tutorial made some sense (not so much into making tuts). I may create a second tutorial that tackles on how to make your inserted field appear in the Administration panel. Until then, ciao!

PS: If you want to learn more about the MVC-L code structure that OpenCart uses please follow this link. Great stuff!

EDIT (08-18-2011): I translated this method of modification to vQmod,  which is a safer way to modify OpenCart core files. Click here:

Part 1:

Adding Custom Fields in OpenCart 1.5 Create Account, Using vQmod

Part 2:

Adding Custom Fields in OpenCart 1.5 Using vQmod: Administration Panel

About maca

Hello, my name is Karlo Macariola. I once dreamed to be a painter or an animator but when I typed my first line of code I immediately fell in love with programming. Today my passions are with web development and web design though sometimes I still withdraw to my first love which is drawing. DeviantArt | Twitter | Facebook
This entry was posted in Web Development & Design and tagged , , . Bookmark the permalink.

21 Responses to How to Add Custom Fields to Customers in OpenCart 1.5

  1. Pingback: Adding Custom Fields in OpenCart 1.5 Create Account, Using vQmod | Maca is Rambling

  2. Noid Lin says:

    So, I can add a new field in products pages using the similar method, am I right?

  3. Sasa says:

    I need your help about vQmod.
    Please contact me on kasapic@gmail.com
    Best regards.
    http://www.negotin.biz/kasapa

  4. Rene says:

    Hi, I would like to ask you if second tutorial is made or not about: how to make your inserted field appear in the Administration panel
    Thank you

  5. Cristian says:

    Great Tutorial !
    PS: Are you italian ? Just out of curiosity 🙂

  6. Toren says:

    Excelent! Thanks 🙂

  7. Anton says:

    Heyz great tut, gonna try to implement this myselfs tonight.
    I am just wondering does teh salutation also appear on the invoice?
    Or do you happen to know how to get it into the invoices?
    Well i do know how to get stuff in and out of the invoice but i need the php echo code for it and i am to newbie to figure out what to use.
    Now in the invoice, stuff is called upon with will editing that to work?

    Greetings,
    Anton

  8. Anton says:

    ow the codes didnt get through… hopefully the following will work.
    Code: starting with ofcourse
    php echo $order[‘invoice_no’];

  9. Atticuss says:

    Hi,

    i not quite understand about step 1. Do i need to edit the file in public_html/system/database? or do create a new file?

  10. soumen roy says:

    How can I add a file input field to upload photo or word doc and how can i get the value of that field in the controller file to update/insert in database.
    Thank you.

  11. Pingback: Adding new drop down in registration | Open Cart Know How

  12. LE DUNG says:

    Thanks! Great Tutorial.!

  13. Mihir Bhavsar says:

    Great Post! Thank you! I was able to add a new field to my “register” page. However, I would appreciate if you could guide me to do the same in the new shipping address page in checkout.
    Thanks.

  14. Sunny Thakur says:

    Helo sir,,
    Can u gve me a favour,,,
    I am using opencart
    I have add 3 new fields in product table and placed values in it,,,,,its all done very well like insertion and deletion,,,,,,,,,,,,,, but problem is that when i m going to edit any product that custom fields values are not show in the textbox,,,,,,,,,,,, can u please help me????????

  15. Christian MIko says:

    Kababayan maraming salamat sa tutorial mo. Malaking tulong to para sakin 🙂 Ito lang ang post na nakita ko sa google na nagtuturo kung paano icustomize ang Opencart sa madaling paraan. Karamihan kasi may bayad. Mabuhay ka kababayan.

    Mabuhay ang mga taong tulad mo na handang tumulong at mamahagi ng kaalaman kahit walang kapalit.

  16. Andrew Valentinea says:

    Great mod, REALLY HANDY! Is there a mod so the new field is seen on the EDIT ACCOUNT page?

Leave a Reply to maca Cancel reply

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