Today we will find out how to add a new field in our OpenCart Admin. Sometimes, according to the project, we need new fields to be added – new product types, new product information that we need and want to display, new category or manufacturers fields. So lets create a new product field.
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']) . "',
and then you should add the same code in the product edit funtion – UPDATE query and we are done with the model file.
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:
if (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'] = ''; }
4. View
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:
<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>
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.
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.
this one’s very interesting.http://www.lixeiraplastica.com
Wonderful points altogether, you just received a new reader. What might you suggest in regards to your submit that you just made a few days ago? Any positive?
Hello,
Thank you for “How to add custom fields in OpenCart?”
I went through it all and managed to get it up in the admin Product Data page but unfortunately with this error displayed in the custom field I added:
I have tried to find out how to correct the error but not been successful.
Please help me out.
Thanks and best regards,
Jason
Sorry, but somehow your comment went into a spam folder. If you still need help, please explain me what error exactly you got.
I have been struggling with these instructions all day. It would be really helpful if you specified the exact files that needed editing. You say to edit the model file in the model folder but there is 10 folders with up to 11 files in each folder in the admin model folder. So which file needs to be edited?
Hi Jack, unfortunately that tutorial was written years ago and I am not sure if it can help you with the current OpenCart version, since I am not using OpenCart that often lately. Otherwise, if you still want to try you need to add these in the desired files – if you are adding field to the products it is gonna be the product file in the models folder.