1. Database
First we need to add a field in our database where the information that we need will be stored. If you take a look to the database structure you can see that there are a few different tables where we can add our new info. It is important for you to decide where the field will be added – if it is numerical it can be inserted in the product data table, but if it is text field and you need it translated the description table is better idea, because otherwise you have a lot of work to do to related to the translations and it is not really relevant. So when you decide where want to insert the field we can add it to the base. The SQL statement that you need is something like this:
ALTER TABLE 'product' ADD 'your_field name' VARCHAR (250) NOT NULL AFTER 'model' ; |
Of course in accordance of your needs, you can change the table that is altered, the type of the field and where exactly in the table it will be added.
Now we have a row in our database to save the new info. It is time to create new field in the admin interface to insert this.
2.Model
As we said in the previous tutorial we have four admin folders – controller, model, language and view. Lets start from the model file to create an option for our field to be saved. In the first function INSERT INFO you should add your field
your_field name = '" . $this->db->escape($data[' your_field name ']) . "' , |
3.Controller
Next one is the controller file, where we should define the new field:
$this->data['entry_your_field_name'] =
$this->language->get('entry_your_field_name');
This is where you set the variable and where the file is getting the language value for it.Next piece of code that you have to add is:
4. View
1234567if
(isset(
$this
->request->post[
'your_field name'
])) {
$this
->data[
'your_field name'
] =
$this
->request->post[
'your_field name'
];
}
elseif
(isset(
$product_info
)) {
$this
->data[
'your_field name'
] =
$product_info
[
'your_field name'
];
}
else
{
$this
->data[
'your_field name'
] =
''
;
}
Now we are able to save our new field in the database, but we need to make a visual form for this in the admin panel, so it is time to edit the view. We need the file called “product-form.tlp”. It is located in the folder admin/view/template/catalog and here are the changes that you have to make:
This is the admin front-end code if your field is in “Data” tab in the product Add/Edit form. Of course if you have added the field in any other tab you should change the html around the entry to fit to the template.
12345<tr>
<td><?php
echo
$entry_your_field
name; ?></td>
<td><input type=
"text"
name=
"your_field name"
value=
"<?php echo $your_field name; ?>"
/></td>
</td>
</tr>
And we are ready to go! Now we have a new custom field that describes our product and we can display it in the front-end of our project to add custom information about the products and why not a new functionality? I will describe how this can be done in the next tutorial and I will also provide some examples how you can use that knowledge.