Notes

Upgrading OpenCart Extensions

Edit on GitHub

OpenCart
3 minutes

Coding standards

  • Removed the PHP ending tag ?> from php files

Load the Language File

In 1.5.x, you would use the following snippet to load the corresponding module language file.

1$this->language->load('account/account');

On the other hand, you need to use a slightly different version in the case of OpenCart 2.x.x, as shown in the following snippet.

1$this->load->language('account/account');

Controller

1. Assigning template variables

most of the time you’ll end up assigning your variables so that you can access them in the view template files

OpenCart 1.5:

1$this->data['foo'] = $foo;
2
3$this->data['heading_title'] = $this->language->get('heading_title');

OpenCart 2.0:

1$data['foo'] = $foo;
2
3$data['heading_title'] = $this->language->get('heading_title');

2. Assigning a template

OpenCart 1.5:

1$this->template = 'module/module.tpl';
2
3$this->response->setOutput($this->render()); // OC 1.5.6.x

OpenCart 2.0:

1$this->response->setOutput($this->load->view('module/module', $data));
2
3$this->response->setOutput($this->load->view('default/template/account/account.tpl', $data)); // OC 2.1.0.2
4
5$this->response->setOutput($this->load->view('extension/total/total', $data)); // OC 2.3.0.2+ (no .tpl at the end)

In the earlier version of OpenCart, you would have used the following code to assign the template file and render it.

1if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/account.tpl')) {
2  $this->template = $this->config->get('config_template') . '/template/account/account.tpl';
3} else {
4  $this->template = 'default/template/account/account.tpl';
5}
6$this->response->setOutput($this->render());

In the recent version, you’ll need to use a slightly different and shorter way to handle it.

1if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/account.tpl')) {
2  $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/account.tpl', $data));
3} else {
4  $this->response->setOutput($this->load->view('default/template/account/account.tpl', $data));
5}

As you can see, in the recent version we’re using the view method of the Loader class.

3. Children Template Assignment

In 1.5.x, you needed to provide an array to assign all the children templates like header, footer, column_left, etc.

1$this->children = array(
2  'common/column_left',
3  'common/column_right',
4  'common/content_top',
5  'common/content_bottom',
6  'common/footer',
7  'common/header'       
8);

In the case of the 2.x.x version, you’ll use the controller method of the Loader class. (You can load controllers now)

1$data['column_left'] = $this->load->controller('common/column_left');
2$data['column_right'] = $this->load->controller('common/column_right');
3$data['content_top'] = $this->load->controller('common/content_top');
4$data['content_bottom'] = $this->load->controller('common/content_bottom');
5$data['footer'] = $this->load->controller('common/footer');
6$data['header'] = $this->load->controller('common/header');

4. URL Redirection

OpenCart 1.5:
In the older version, the redirect method was part of the abstract Controller class, so you could call that directly using the $this object.

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

OpenCart 2.0:
In the recent version, the redirect method belongs to the Response class and is thus accessed via that object.

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

5. Breadcrumb

In the recent version, you don’t need to provide a “separator” as an array key as opposed to the earlier version.

Here’s the snippet from the 1.5.x version:

1$this->data['breadcrumbs'][] = array(
2  'text'      => $this->language->get('text_home'),
3  'href'      => $this->url->link('common/home'),
4  'separator' => false
5);

A similar result can be achieved by using the following code:

1$data['breadcrumbs'][] = array(
2  'text' => $this->language->get('text_home'),
3  'href' => $this->url->link('common/home')
4);

6. Status field is mandatory for the module now

1if (isset($this->request->post['moduleName_status'])) {
2	$data['moduleName_status'] = $this->request->post['moduleName_status'];
3} else {
4	$data['moduleName_status'] = $this->config->get('moduleName_status')
5}