• Sumo

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