Categories
Programming

How to add custom filed to customers table in OpenCart – Admin panel

How to insert custom field to customer in Open Cart. Code example for open cart custom field.

How to add custom filed to customers table in OpenCart?

OpenCart has many flaws, at least in version <2. Unlike WordPress for example there is no easy way to change core functionality with custom plugin without changing the source code. There is no pub/sub architecture for interrupting system events…

But if you need to add custom fields to custumer, here is code simple example for that in five simple steps.

Step 1

Add form html in admin panel :


// admin/view/template/sale/customer_form.tpl
// find <input name="firstname" type="text" value="<?php echo $firstname; ?>" />
// and add before :
<input name="#field_name#" type="text" value="<?php echo #field_name#; ?>" />

Step 2

Add translation for field :


// admin/language/english/sale/customer.php
// find 'Entry' part
// Add
$_['entry_#field_name#']   		= 'Some text :';
Step 3

Pull language translation :

    
// admin/controller/sale/customer.php
// find : $this->data['entry_firstname'] = $this->language->get('entry_firstname');
// add before
$this->data['entry_#field_name#'] = $this->language->get('entry_#field_name#');
    
Step 4

Retrive custom field from POST

    
// admin/controller/sale/customer.php
// find : if (isset($this->request->post['firstname'])) {
// add before
if (isset($this->request->post['#field_name#'])) {
   $this->data['#field_name#'] = $this->request->post['#field_name#'];
} elseif (isset($customer_info)) {
   $this->data['#field_name#'] = $customer_info['#field_name#'];
} else {
   $this->data['#field_name#'] = '';
}
Step 5

Insert and update database custom field

    
// admin/model/sale/customer.php
// Find insert query in addCustomer function
// add at the end of query
', #field_name# = '" . $this->db->escape($data['#field_name#']) . "'");

// find update function and add custom field :
', #field_name# = '" . $this->db->escape($data['#field_name#']) . "' WHERE customer_id = '" . (int)$customer_id . "'");
Step 6

Alter customer table with SQL

    
ALTER TABLE  `table_prefix_customer`  ADD  `#field_name#` VARCHAR (250) NOT NULL
    

Replace #field_name# with real name.

Note: this modification will be overridden when you upgrade your OpenCart version. To add automated process of upgrade use vQmod. Here is exmaple of that : http://itechtuts.com/custom-fields-opencart-administration-panel-using-vqmod/

8 replies on “How to add custom filed to customers table in OpenCart – Admin panel”

Thanks for the post, this helped a lot. Now I just have to figure out how to use the information. I added a text box to my customer info. I want to be able to put an image name in there and use that image on the invoice.

Hi Jeff, I am glad that my article provide you at least some help 🙂

If you want to change invoice, look for the template file :

admin\view\template\sale\order_invoice.tpl

and add image tag with that value from customer new field. That template is filled with data in this controller :

admin\controller\sale\order.php 

Take a look at “invoice” function.

Hi Preet,

sorry but I can’t open that link 🙁 . About adding custom tab in admin menu I have never done that so far. I think it’s requires more coding than simple custom field’s adding that are explained in this article.

Best regards,
Nikola

Hi,

Very good post! I know this post is old… But i dont found any doc about OpenCart 3, how add a col in database, product table with qmod or ocmod.
I put it manualy, not good 🙁

ALTER TABLE `table_prefix_product` ADD `mycol` VARCHAR (250)

Could you help or guide me? Any docs

Hi Allan,
I got a way to alter table in oc-mod. Please take a look of my guideline below:

1. In your *ocmod.zip file, create files with following structure:
– ./upload/admin/controller/extension/module/.php
– ./upload/admin/language/en-gb/extension/module.php
– install.xml

2. In ./upload/admin/controller/extension/module/.php, implement following format:

language->get(‘heading_title’); // It will show your module name in modules list
}

public function install() {
// alter table
$this->db->query(“ALTER TABLE ” . DB_PREFIX . “table_name ADD COLUMN IF NOT EXISTS column_name VARCHAR (250);”);
}

public function uninstall() {
$this->db->query(“ALTER TABLE ” . DB_PREFIX . “table_name DROP IF EXISTS column_name;”);
}
}

3. In ./upload/admin/language/en-gb/extension/module.php, implement following format:

Installer -> Upload -> Choose your *.ocmod.zip.
Then, go to Extensions -> Modifications. Click Refresh button in the top right screen.

5. Install Module
In the admin site, select Extensions -> Extensions. Choose “Modules” in Extension type filter.
Find your module name and click install button in Action column. It will run your install() function. Otherwise, click uninstall button, it will run uninstall() function.

Hope this will help you !!!

Leave a Reply

Your email address will not be published. Required fields are marked *