Page 1 of 1

Custom Fields in Account Create

Posted: Sat Dec 20, 2008 8:14 am
by ~DareErica
I need to add more fields to the form in "account_create"... I would also like theat information to be sent to the database like the rest of the fields and possibly make some of the new fields required. Does anyone know how I would get started on this? I took a look at account_create.tpl and can probabaly get the fields in, but i don't know about the database work.

Thanks

Re: Custom Fields in Account Create

Posted: Sat Dec 20, 2008 8:37 am
by jty
Study account_create.php
Find a field that already exists eg firstname or another one and copy that field
Put your new_field wherever you see the firstname or whatever field
Do the same for account_create.tpl
You will need to add the fields into your database

And the golden rule is ................... BACKUP !!!!! and never give up

Re: Custom Fields in Account Create

Posted: Sat Dec 20, 2008 10:59 am
by Qphoria
Hi and welcome to OpenCart :)

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.

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(s)

STEP 1:
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 table exists in the database.

STEP 2:
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'),

STEP 3:
Set up the language file. This part is quite easy. Simply edit the catalog/language/english/controller/account_create.php file:
Find:
$_['entry_firstname']      = 'First Name:';
Add this below it:
$_['entry_salutation']      = 'Salutation:';

Done!

STEP 4:
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

<tr>
    <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>
</tr>
So lets adapt that for the new field:

Code: Select all

<tr>
    <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>
</tr>
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.

Re: Custom Fields in Account Create

Posted: Wed Jan 07, 2009 12:55 am
by ~DareErica
Thanks jty and Qphoria for your replies. I followed the directions Qphoria gave. I put everything on catalog/controller/account_create.php  in the same order that they are on the database but i still get an error:

Warning: vsprintf() [function.vsprintf]: Too few arguments in /Applications/MAMP/htdocs/opencart/upload/library/database/database.php on line 43
Error: Query was empty
Error No: 1065

What can I do to fix this? It does this when I add the fields to line 29 of the catalog/controller/account_create.php file. It is fine without adding to this line but it does not add the info to the database fields.

Re: Custom Fields in Account Create

Posted: Wed Jan 07, 2009 1:18 am
by Qphoria
Be sure that for every '?' in line 28,
there is a corresponding value in line 29

I truncated the line a bit for the demo, but there are like 20 fields that get pulled here

For example, match the '?' colors to the value colors:
line 28: $sql = "insert into customer set firstname = '?', salutation = '?', lastname = '?', email = '?',.......

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

Re: Custom Fields in Account Create

Posted: Wed Jan 07, 2009 2:44 am
by ~DareErica
Thanks. There were a couple missing values from line 29 that are on line 28 so I added them. It took care of the other error but I get this error now:

Error: Unknown column 'established' in 'field list'
Error No: 1054
insert into address set customer_id = '', firstname = 'test2', lastname = 'test2', company = 'test2', address_1 = 'test2', address_2 = 'test2', city = 'test2', postcode = '999999', country_id = '223', zone_id = '3618', established = '2020', motto = 'test2', languages = 'test2', operation = 'test2'

Any idea how I could fix this? I'm adding about 5 fields to the form

Re: Custom Fields in Account Create

Posted: Wed Jan 07, 2009 2:56 am
by Qphoria
You need to do Step 1 for each field you are adding. The insert can't find the field in the database because you didn't add it yet

Re: Custom Fields in Account Create

Posted: Wed Jan 07, 2009 3:01 am
by ~DareErica
they were added but maybe not in the right order? I rearranged them and now I get this error:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Error No: 1064
insert into customer set firstname = 'test4', lastname = 'test4', email = 'test4@test4.com', telephone = 'test4', alt_phone = 'test4', position = 'test4', established = 'test4', motto = 'test4', languages = 'test4', operation = 'test4', password = '86985e105f79b95d6bc918fb45ec7727', newsletter = '0', status = '1', date_added = now(),