Upgrading modules from OpenCart 1.5.x to OpenCart 2.0.x or changes in the OpenCart 2.0.x

Sorry for my English! English is not my native language. One of the reasons to create this blog is to improve my English writing. So I will be highly obliged if you will help me with this. If you find a grammar error on this page, please select it with your mouse and press Ctrl+Enter.

OpenCart 2.0 has been released and it is very different from OpenCart 1.5. Extensions from 1.5 will not work on the 2.0. You need to upgrade them from OpenCart 1.5 to OpenCart 2.0. I decided to write this article where I am planning to describe how to do it and describe differences in the OpenCart 2.0.

I can't give you all the differences in the OpenCart 2.0, because OpenCart is quite a big system and it is always changing. I will give you changes that I discovered.
 
Changes:
 
  • Coding standards. They changed a little. You don't need to put PHP ending tag "?>" In the end of your PHP files. This is correct, many CMS doesn't use this tag for a long time. 
  • bootstrap It'll take you most of the time. What is bootstrap? It is a css framework that helps to build user interfaces with tables, buttons, grids etc. It is really very useful. You don't need to create html and css code for every device and screen resolution. The bootstrap will do it for you. But it will take you time to learn how it works. 
    You can read about bootstrap here. If you don't know how to build something using the bootstrap, I'll give you an advice. Just open the admin product form template and look how it is created. The product form template is a very big template with a big amount of elements. It probably contains the element what you need to create. 
  • OCMOD instead of VQMOD. It is almost the same. It has similar syntax, but OCMOD is built in the OpenCart. OCMOD planned to be more simple for reduce amount of the conflicts. For example, it wasn't supposed to contain the offsets. But after several commits it contains offsets now. So OCMOD has practically the same functionality as the VQMOD. 
  • Events As for me, the Events are the biggest innovation in the OpenCart 2.0. Events give you ability to change OpenCart logic from your modules without changing the OpenCart core files.  The almost every modern CMS has similar functionality. For example Drupal has the hook system. The Events in the OpenCart are in under development now, they are not covering all OpenCart yet, but I hope that they will improve and in some time they will replace the VQMOD (OCMOD). 
  • The controller
1. Assigning template variables  
OpenCart 1.5:

$this->data['foo'] = $foo;

OpenCart 2.0:

$data['foo'] = $foo;

 
2. Assigning a template
OpenCart 1.5:

$this->template = 'module/module.tpl';

OpenCart 2.0:

$this->response->setOutput($this->load->view('module/module', $data));

 
3. Adding child controllers
OpenCart 1.5:

$this->children = array(
  'common/header',
  'common/footer'
);

OpenCart 2.0 (you can load controllers now):

$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

 
4. Redirect
OpenCart 1.5:

$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));

OpenCart 2.0:

$this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));

 
5. Status field is mandatory for the module now:

if (isset($this->request->post['module_name_status'])) {
  $data['featured_status'] = $this->request->post['module_name_status'];
} else {
  $data['featured_status'] = $this->config->get('module_name_status');
}

 
If during upgrade your modules you will find a new change not described here, please write a comment and I will add it in the article.
 

Comments

<p>You sir are a saint. Thank you so much for this article, I cannot express how valuable it is to me</p>

Thank you so much .....

Hello sir, how are you? i hope you are doing fine. you are genius. i have upgraded a module in opencart 2.0, the admin section is perfect, but it will not showing on the front section of the store. please help me.. The code is as given below: 1.) /admin/controller/module/eassistancepro_livechat.php <?php /* * @package eAssistance Pro Live Chat * @version 1.0 */ class ControllerModuleeassistanceprolivechat extends Controller { private $error = array(); public function index() { $this->language->load('module/eassistancepro_livechat'); $this->document->setTitle($this->language->get('heading_title')); $this->load->model('setting/setting'); if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { $this->model_setting_setting->editSetting('eassistancepro_livechat', $this->request->post); $this->session->data['success'] = $this->language->get('text_success'); //$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')); $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')); } $data['heading_title'] = $this->language->get('heading_title'); $data['text_enabled'] = $this->language->get('text_enabled'); $data['text_disabled'] = $this->language->get('text_disabled'); $data['text_content_top'] = $this->language->get('text_content_top'); $data['text_content_bottom'] = $this->language->get('text_content_bottom'); $data['text_column_left'] = $this->language->get('text_column_left'); $data['text_column_right'] = $this->language->get('text_column_right'); $data['entry_code'] = $this->language->get('entry_code'); $data['entry_layout'] = $this->language->get('entry_layout'); $data['entry_position'] = $this->language->get('entry_position'); $data['entry_status'] = $this->language->get('entry_status'); $data['entry_sort_order'] = $this->language->get('entry_sort_order'); $data['button_save'] = $this->language->get('button_save'); $data['button_cancel'] = $this->language->get('button_cancel'); $data['button_add_module'] = $this->language->get('button_add_module'); $data['button_remove'] = $this->language->get('button_remove'); if (isset($this->error['warning'])) { $data['error_warning'] = $this->error['warning']; } else { $data['error_warning'] = ''; } if (isset($this->error['code'])) { $data['error_code'] = $this->error['code']; } else { $data['error_code'] = ''; } $data['breadcrumbs'] = array(); $data['breadcrumbs'][] = array( 'text' => $this->language->get('text_home'), 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'), 'separator' => false ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('text_module'), 'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'), 'separator' => ' :: ' ); $data['breadcrumbs'][] = array( 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('module/eassistancepro_livechat', 'token=' . $this->session->data['token'], 'SSL'), 'separator' => ' :: ' ); $data['action'] = $this->url->link('module/eassistancepro_livechat', 'token=' . $this->session->data['token'], 'SSL'); $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'); if (isset($this->request->post['eassistancepro_livechat_code'])) { $data['eassistancepro_livechat_code'] = $this->request->post['eassistancepro_livechat_code']; } else { $data['eassistancepro_livechat_code'] = $this->config->get('eassistancepro_livechat_code'); } $data['modules'] = array(); if (isset($this->request->post['eassistancepro_livechat_module'])) { $data['modules'] = $this->request->post['eassistancepro_livechat_module']; } elseif ($this->config->get('eassistancepro_livechat_module')) { $data['modules'] = $this->config->get('eassistancepro_livechat_module'); } $this->load->model('design/layout'); $data['layouts'] = $this->model_design_layout->getLayouts(); $this->template = 'module/eassistancepro_livechat.tpl'; /* $this->children = array( 'common/header', 'common/footer' ); */ $data['heading_title'] = $this->language->get('heading_title'); $data['header'] = $this->load->controller('common/header'); $data['column_left'] = $this->load->controller('common/column_left'); $data['footer'] = $this->load->controller('common/footer'); //$this->response->setOutput($this->render()); $this->response->setOutput($this->load->view($this->template, $data)); } protected function validate() { if (!$this->user->hasPermission('modify', 'module/eassistancepro_livechat')) { $this->error['warning'] = $this->language->get('error_permission'); } if (!$this->request->post['eassistancepro_livechat_code']) { $this->error['code'] = $this->language->get('error_code'); } if (!$this->error) { return true; } else { return false; } } } 2.) /admin/view/template/module/eassistancepro_livechat.tpl <!-- * @package eAssistance Pro Live Chat * @version 1.0 --> <?php echo $header; ?> <?php echo $column_left; ?> <div id="content"> <div class="page-header"> <div class="container-fluid"> <div class="pull-right"> <button class="btn btn-primary" title="" data-toggle="tooltip" form="form-slideshow" type="submit" data-original-title="<?php echo $button_save; ?>" onclick="$('#form').submit();"><i class="fa fa-save"></i></button> <a class="btn btn-default" title="" data-toggle="tooltip" data-original-title="<?php echo $button_cancel; ?>" href="<?php echo $cancel; ?>"><i class="fa fa-reply"></i></a> </div> <h1><?php echo $heading_title; ?></h1> <ul class="breadcrumb"> <?php foreach ($breadcrumbs as $breadcrumb) { ?> <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?> </a></li> <?php } ?> </ul> </div> </div> <div class="container-fluid"> <?php if ($error_warning) { ?> <div class="warning"> <?php echo $error_warning; ?> </div> <?php } ?> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"><i class="fa fa-pencil"></i><?php echo "Edit " . $heading_title . " Module"; ?></h3> </div> <div class="panel-body"> <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form"> <div class="well"> <div class="row"> <div class="col-sm-12"> <div class="form-group"> <label for="input-name" class="col-sm-2 control-label"><?php echo $entry_code; ?></label> <div class="col-sm-10"> <textarea name="eassistancepro_livechat_code" cols="40" rows="5" placeholder="Please enter eassistancepro chat code here" class="form-control"><?php echo $eassistancepro_livechat_code; ?></textarea> <?php if ($error_code) { ?> <span class="error"><?php echo $error_code; ?></span> <?php } ?></td> </div> </div> </div> </div> </div> <div class="table-responsive"> <table class="table table-bordered table-hover"> <thead> <tr> <td class="text-center" style="width: 1px;"> <!--<input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);">--> </td> <td class="text-left"><?php echo $entry_layout; ?></td> <td class="text-left"><?php echo $entry_position; ?></td> <td class="text-left"><?php echo $entry_status; ?></td> <td class="text-center"><?php echo $entry_sort_order; ?></td> <td class="text-center">Action</td> </tr> </thead> <?php $module_row = 0; if(count($modules) > 0) { foreach ($modules as $module) { ?> <tbody class="modulebox"> <tr id="module-row<?php echo $module_row; ?>"> <td class="text-center"></td> <td class="text-left"> <select name="eassistancepro_livechat_module[<?php echo $module_row; ?>][layout_id]"> <?php foreach ($layouts as $layout) { ?> <?php if ($layout['layout_id'] == $module['layout_id']) { ?> <option value="<?php echo $layout['layout_id']; ?>" selected="selected"> <?php echo $layout['name']; ?> </option> <?php } else { ?> <option value="<?php echo $layout['layout_id']; ?>"> <?php echo $layout['name']; ?> </option> <?php } ?> <?php } ?> </select> </td> <td class="text-left"> <select name="eassistancepro_livechat_module[<?php echo $module_row; ?>][position]"> <?php if ($module['position'] == 'content_top') { ?> <option value="content_top" selected="selected"> <?php echo $text_content_top; ?> </option> <?php } else { ?> <option value="content_top"> <?php echo $text_content_top; ?> </option> <?php } ?> <?php if ($module['position'] == 'content_bottom') { ?> <option value="content_bottom" selected="selected"> <?php echo $text_content_bottom; ?> </option> <?php } else { ?> <option value="content_bottom"> <?php echo $text_content_bottom; ?> </option> <?php } ?> <?php if ($module['position'] == 'column_left') { ?> <option value="column_left" selected="selected"> <?php echo $text_column_left; ?> </option> <?php } else { ?> <option value="column_left"> <?php echo $text_column_left; ?> </option> <?php } ?> <?php if ($module['position'] == 'column_right') { ?> <option value="column_right" selected="selected"> <?php echo $text_column_right; ?> </option> <?php } else { ?> <option value="column_right"> <?php echo $text_column_right; ?> </option> <?php } ?> </select> </td> <td class="text-left"> <select name="eassistancepro_livechat_module[<?php echo $module_row; ?>][status]"> <?php if ($module['status']) { ?> <option value="1" selected="selected"> <?php echo $text_enabled; ?> </option> <option value="0"> <?php echo $text_disabled; ?> </option> <?php } else { ?> <option value="1"> <?php echo $text_enabled; ?> </option> <option value="0" selected="selected"> <?php echo $text_disabled; ?> </option> <?php } ?> </select> </td> <td class="text-center"> <input type="text" name="eassistancepro_livechat_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /></td> <td class="text-left"> <a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><span><?php echo $button_remove; ?></span></a> </td> </tr> </tbody> <?php $module_row++; } } ?> <tfoot> <tr> <td class="text-left" colspan="5">&nbsp;</td> <td class="text-left"> <a onclick="addModule();" class="button" href="javascript:;"><span><?php echo $button_add_module; ?> </span> </a> </td> </tr> </tfoot> </table> </div> </form> <div class="row"> <div class="col-sm-6 text-left"></div> <div class="col-sm-6 text-right">&nbsp;</div> </div> </div> </div> </div> </div> <script type="text/javascript"> var module_row = '<?php echo $module_row; ?>'; module_row = parseInt(module_row); function addModule() { var html = '<tr id="module-row' + module_row + '">'; html += '<td class="text-left">&nbsp;</td>'; html += '<td class="text-left"><select name="eassistancepro_livechat_module[' + module_row + '][layout_id]">'; <?php foreach ($layouts as $layout) { ?> html += '<option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>'; <?php } ?> html += '</select></td>'; html += '<td class="text-left"><select name="eassistancepro_livechat_module[' + module_row + '][position]">'; html += '<option value="content_top"><?php echo $text_content_top; ?></option>'; html += '<option value="content_bottom"><?php echo $text_content_bottom; ?></option>'; html += '<option value="column_left"><?php echo $text_column_left; ?></option>'; html += '<option value="column_right"><?php echo $text_column_right; ?></option>'; html += '</select></td>'; html += '<td class="text-left"><select name="eassistancepro_livechat_module[' + module_row + '][status]">'; html += '<option value="1" selected="selected"><?php echo $text_enabled; ?></option>'; html += '<option value="0"><?php echo $text_disabled; ?></option>'; html += '</select></td>'; html += '<td class="text-center"><input type="text" name="eassistancepro_livechat_module[' + module_row + '][sort_order]" value="" size="3" /></td>'; html += '<td class="text-left"><a onclick="$(\'#module-row' + module_row + '\').remove();" class="button"><span><?php echo $button_remove; ?></span></a></td>'; html += '</tr>'; $("div.table-responsive table").append(html); module_row++; } </script> <?php echo $footer; ?> 3.) /catelog/controller/module/eassistancepro_livechat.php <?php class ControllerModuleeassistanceprolivechat extends Controller { public function index() { $this->language->load('module/eassistancepro_livechat'); $data['heading_title'] = $this->language->get('heading_title'); if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) { $data['code'] = str_replace('http', 'https', html_entity_decode($this->config->get('eassistancepro_livechat_code'))); } else { $data['code'] = html_entity_decode($this->config->get('eassistancepro_livechat_code')); } if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/eassistancepro_livechat.tpl')) { return $this->response->setOutput($this->config->get('config_template') . '/template/module/eassistancepro_livechat.tpl', $data); } else { return $this->response->setOutput('default/template/module/eassistancepro_livechat.tpl', $data); } } } ?> 4.) /catalog/view/theme/default/template/module/eassistancepro_livechat.tpl <!-- eassistancepro.com chat code start here --> <?php echo $code; ?> <!-- eassistancepro.com chat code end here --> Thank you so much.... i think everything is right accordingly opencart2.0, but not showing the module in front of the site. please help me..

Add new comment

CAPTCHA
Spam protection
Target Image