ArticleUpgrading 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

ocart hacker wrote:

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

05.02.2015 00:36
arihant wrote:
Thank you so much .....
18.02.2015 08:52
Praveen Kumar wrote:
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 <?php echo $header; ?> <?php echo $column_left; ?>
<?php if ($error_warning) { ?>
<?php echo $error_warning; ?>
<?php } ?>
<?php echo "Edit " . $heading_title . " Module"; ?>
" method="post" enctype="multipart/form-data" id="form">
<?php echo $entry_code; ?>
<?php echo $eassistancepro_livechat_code; ?> <?php if ($error_code) { ?> <?php echo $error_code; ?> <?php } ?>
<?php echo $entry_layout; ?> <?php echo $entry_position; ?> <?php echo $entry_status; ?> <?php echo $entry_sort_order; ?> Action <?php $module_row = 0; if(count($modules) > 0) { foreach ($modules as $module) { ?> "> ][layout_id]"> <?php foreach ($layouts as $layout) { ?> <?php if ($layout['layout_id'] == $module['layout_id']) { ?> " selected="selected"> <?php echo $layout['name']; ?> <?php } else { ?> "> <?php echo $layout['name']; ?> <?php } ?> <?php } ?> ][position]"> <?php if ($module['position'] == 'content_top') { ?> <?php echo $text_content_top; ?> <?php } else { ?> <?php echo $text_content_top; ?> <?php } ?> <?php if ($module['position'] == 'content_bottom') { ?> <?php echo $text_content_bottom; ?> <?php } else { ?> <?php echo $text_content_bottom; ?> <?php } ?> <?php if ($module['position'] == 'column_left') { ?> <?php echo $text_column_left; ?> <?php } else { ?> <?php echo $text_column_left; ?> <?php } ?> <?php if ($module['position'] == 'column_right') { ?> <?php echo $text_column_right; ?> <?php } else { ?> <?php echo $text_column_right; ?> <?php } ?> ][status]"> <?php if ($module['status']) { ?> <?php echo $text_enabled; ?> <?php echo $text_disabled; ?> <?php } else { ?> <?php echo $text_enabled; ?> <?php echo $text_disabled; ?> <?php } ?> ][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /> ').remove();" class="button"><?php echo $button_remove; ?> <?php $module_row++; } } ?>   <?php echo $button_add_module; ?>
 
var module_row = '<?php echo $module_row; ?>'; module_row = parseInt(module_row); function addModule() { var html = ''; html += ' '; html += ''; <?php foreach ($layouts as $layout) { ?> html += '"><?php echo $layout['name']; ?>'; <?php } ?> html += ''; html += ''; html += '<?php echo $text_content_top; ?>'; html += '<?php echo $text_content_bottom; ?>'; html += '<?php echo $text_column_left; ?>'; html += '<?php echo $text_column_right; ?>'; html += ''; html += ''; html += '<?php echo $text_enabled; ?>'; html += '<?php echo $text_disabled; ?>'; html += ''; html += ''; html += '<?php echo $button_remove; ?>'; html += ''; $("div.table-responsive table").append(html); module_row++; } <?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 <?php echo $code; ?> 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..
30.04.2015 10:08

Add new comment

CAPTCHA
Spam protection
Image CAPTCHA
Enter the characters shown in the image.
You are reporting a typo in the following text:
Simply click the "Send typo report" button to complete the report. You can also include a comment.