Wednesday 15 July 2015

Add Custom Product Field in Opencart

In this tutorial we are going to learn how to add custom product field in opencart. Sometimes, due to specific requirement of the project, we need add new product fields which can be – product types, product information, custom description etc., that we want to display. So lets create a new product field called"custom_desc".

 Note : Following changes are tested in Opencart Versions : 2.0.1.1 and 2.0.3.1 


To accomplish our goal we need to make the changes at two levels
    - Database level
    - Files level


 Note: Before making any changes directly at any levels , its recommended to take a Backup of database and files.

(1) Database Level




In order to add the new field you can manually add it via phpmyadmin interface or by running the sql command given below. 



ALTER TABLE `oc_product` ADD `custom_desc` VARCHAR(200) NULL ;


here "oc_" is table prefix , it might possible that you have the different one than i have used here. In that case simply change it with your prefix or if you don't used any prefix then simply remove it. 


According to your requirements you can alter the query to add any kind of custom product field. So the above statement will add a new product field called "custom_desc" in your product table.


(2) Files Level


Now we need to make the changes required at files (coding) level. First of all we need to alter the insert and update queries for the product information.


Open your admin/model/catalog/product.php file and add the below line of code after the sort_order field in the addProduct($data) function's insert query. (approx line no : 6)


custom_desc = '" . $this->db->escape($data['custom_desc']) . "'

after adding the above code, the complete insert query will be like this:
$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', custom_desc = '" . $this->db->escape($data['custom_desc']) . "', date_added = NOW()");


Now search for the editProduct($product_id, $data) function and add the same code in update query  (approx line no : 131) after the sort_order field,


custom_desc = '" . $this->db->escape($data['custom_desc']) . "'


after adding the above code, the complete update query will be like this:
$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', custom_desc = '" . $this->db->escape($data['custom_desc']) . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");


Now open admin/controller/catalog/product.php  file and search for the following line in the getForm()function.(approx line no : 584)    


$data['entry_sort_order'] = $this->language->get('entry_sort_order');


and add the below line just after that,
$data['entry_custom_desc'] = $this->language->get('entry_custom_desc');


If you want to provide help text for your field on mouse hover then search for the below code (approx line no : 601) ,


$data['help_isbn'] = $this->language->get('help_isbn');


and add the below code just after that,
$data['help_custom_desc'] = $this->language->get('help_custom_desc');



Next search for this code, in the same function, (approx line no : 806)

if (isset($this->request->post['isbn'])) {
 $data['isbn'] = $this->request->post['isbn'];
} elseif (!empty($product_info)) {
 $data['isbn'] = $product_info['isbn'];
} else {
 $data['isbn'] = '';
}


and add the below code just after that,


if (isset($this->request->post['custom_desc'])) {
 $data['custom_desc'] = $this->request->post['custom_desc'];
} elseif (!empty($product_info)) {
 $data['custom_desc'] = $product_info['custom_desc'];
} else {
 $data['custom_desc'] = '';
}


Now open admin/view/template/catalog/product_form.tpl file, search for the below line: (approx line no : 97)
<div class="tab-pane" id="tab-data">


and add the following code block just after the image block in order to add our custom field.

<div class="form-group">
     <label class="col-sm-2 control-label" for="input-custom_desc">
         <span data-toggle="tooltip" title="<?php echo $help_custom_desc; ?>"><?php echo $entry_custom_desc; ?></span>
     </label>
     <div class="col-sm-10">
        <input type="text" name="custom_desc" value="<?php echo $custom_desc; ?>" placeholder="<?php echo $entry_custom_desc; ?>" id="input-custom_desc" class="form-control" />
     </div>
</div>



Now open admin/language/english/catalog/product.php   file and add the following lines,


// in Entry section
$_['entry_custom_desc']      = 'Custom Description';
  
  and 
  
// in Help section
$_['help_custom_desc']       = 'This is Custom Description Field';



Thats it..!! you have successfully added your custom product field in admin section. Go to admin panel and add/edit any product you will find new custom desc field will be added.


 Note :  This newly added field will be shown in the Data tab of the product page of admin panel.

No comments:

Post a Comment