Post by Qphoria » Wed Jan 07, 2009 3:06 am

Adding custom fields isn't too hard, once you get the hang of the structure

First lets add one field to get started so we can see how the structure works. We will add a field called "Salutation" to the account_create page (for Mr, Mrs, Dr, etc.).

There are 3 main files to account_create
The Controller: /catalog/controller/account_create.php - This file does all the leg work for what fields are going to be available on this page as well as handle the interfacing to the database.
The Language: /catalog/language/english/controller/account_create.php - This is the language translation file. All places you see text is referenced in the code by a language constant so no matter what the language is, there need be only one reference label.
The View: /catalog/template/default/content/account_create.tpl - This file is the html display code that takes the variables passed from the controller and language file and is designed the way you'd like to display them.

There is also the database that we need to add a column to for the new field

So first we want to add to column to the database. It's on account_create so we can assume that the new column will be dealing with customer information. So looking at the customer table in the database, lets add a new field called: salutation

We need to either add it manually via phpmyadmin or use a sql entry like this:

Code: Select all

ALTER TABLE `customer` ADD `salutation` VARCHAR( 4 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;
Now the new column has been added to the table in the database.

Next, set the references to the new field in the controller. Use an existing field as a guide. For example, Search the catalog/controller/account_create.php file for "firstname". Try to follow the context of each use by the surrounding code and mimic that entry.
Some examples:
Line 28: $sql = "insert into customer set firstname = '?', lastname = '?', email = '?'.................."
change to: $sql = "insert into customer set firstname = '?', salutation = '?', lastname = '?', email = '?'..................."

Line 29: $database->query($database->parse($sql, $request->gethtml('firstname', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('email', 'post'),
change to: $database->query($database->parse($sql, $request->gethtml('firstname', 'post'), $request->gethtml('salutation', 'post'), $request->gethtml('lastname', 'post'), $request->gethtml('email', 'post'),

Lining them up:
Be sure that for every '?' in line 28,
there is a corresponding value in line 29

For example, match the '?' colors to the value colors:

Code: Select all

line 28: $sql = "insert into customer set firstname = '[color=red]?[/color]', salutation = '[color=blue]?[/color]', lastname = '[color=green]?[/color]', email = '[color=brown]?[/color]',.......

Code: Select all

line 29: $database->query($database->parse($sql, [color=red]$request->gethtml('firstname', 'post')[/color], [color=blue]$request->gethtml('salutation', 'post')[/color], [color=green]$request->gethtml('lastname', 'post')[/color], [color=brown]$request->gethtml('email', 'post')[/color],..........
Set up the language file. This part is quite easy. Simply edit the catalog/language/english/controller/account_create.php file:
$_['entry_firstname']      = 'First Name:';
Add this below it:
$_['entry_salutation']      = 'Salutation:';


Finally you need to display this new field. To do this we will do the same thing from step 2 where we use another field as our guide. Once again, we can use "firstname". Open the catalog/template/default/content/account_create.tpl file and look for "firstname".
We find a section of a table using firstname pretty regularly:

Code: Select all

    <td width="150"><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>
So lets adapt that for the new field:

Code: Select all

    <td width="150"><span class="required">*</span> <?php echo $entry_salutation; ?></td>
    <td><input type="text" name="salutation" value="<?php echo $salutation; ?>">
    <?php if ($error_salutation) { ?>
    <span class="error"><?php echo $error_salutation; ?></span>
    <?php } ?></td>
This field is also a required field, so by following it as our guide, we also set it up to be required as an added bonus.

Now save the files and look at the account create page on your site. With any luck, you should see the new field and it should allow you to enter some text there and save to the new database field.

Note: Even tho we have this new field added and it works on the front end, none of the order steps, account address pages, or backend pages have any reference to "salutation" so these would all have to be edited as well to support this new field.
Last edited by Qphoria on Wed Jan 07, 2009 4:52 am, edited 1 time in total.

Donate!|OpenCart Basics|GeoZones

User avatar


Tue Jul 22, 2008 3:02 am
Who is online

Users browsing this forum: No registered users and 2 guests