We Updated 100 OpenCart Modules From 2.1.0.2 to 2.2. Here is What We Learned

Having a big module portfolio surely brings up towards your image as a software company. From a customer perception, my first thought when I see a dev company with a lot of cool things is...

“Wow, this is awesome. They should be pretty good at it”.

Being in the same sector my second thought is, “How do they support this?”. While this is a tricky one, another thought slips in my mind...

“How do they handle future updates?”

From the perspective of a typical SaaS company, this may not be that much of a hassle. However, since we develop OpenCart extensions there is another layer of the development cycle which is - platform updates.

The most recent OpenCart update 2.2 is without a doubt a step forward. Still it introduced quite a lot of changes, which meant that we had to rework almost all of our modules in order to make them compatible with OpenCart 2.2.

It took us nearly 3 months, lots of coding and a shared spreadsheet, where we logged all the key changes, to update more than 100+ iSenseLabs modules. As an added bonus, we will not only list the key changes you should know about but also propose fixes for them.

1. Simplified template loading in the new OpenCart

To put it short, the way of loading the templates is changed in OpenCart 2.2. Here you can see some of the ways to make compatible with all current releases of OpenCart 2.

Possible fix:

if(version_compare(VERSION, '2.2.0.0', "<")) {
   if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/icolorpicker.tpl')) {
       return $this->load->view($this->config->get('config_template') . '/template/module/icolorpicker.tpl', $this->data);
    } else {
        return $this->load->view('default/template/module/icolorpicker.tpl', $data);
    }
} else {
      return $this->load->view('module/icolorpicker', $data);
}

Possible fix 2:

if(version_compare(VERSION, '2.2.0.0', '<')) {
     $curent_template = $this->config->get('config_template');
} else {
     $curent_template = $this->config->get($this->config->get('config_theme') . '_directory');
}

Possible fix 3:

private function getConfigTemplate() {
    if(version_compare(VERSION, '2.2.0.0', '<')) {
        return $this->config->get('config_template');
    } else {
        return  $this->config->get($this->config->get('config_theme') . '_directory');
    }
}

private function getOutput($TPL_name , $data){
    if(version_compare(VERSION, '2.2.0.0', "<")) {
        if (file_exists(DIR_TEMPLATE . $this->getConfigTemplate() . '/template/module/'. $TPL_name)) {
            return $this->load->view($this->getConfigTemplate().'/template/module/'. $TPL_name, $data);
        } else {
            return $this->load->view('default/template/'. $TPL_name, $data);
        }  
    } else {
        return $this->load->view($TPL_name , $data);
    }
}

2. Changed behavior for the 'skip' argument in OCMOD

The error=”skip” now works differently. Starting from OpenCart 2.2.x, only the operation with the 'error' tag will be skipped, not the entire file. The other tricky part is that if you leave the operation without error instruction, then the file will be skipped. Again, this is valid only for OpenCart 2.2.

3. Language changes

There is a slight changes in the language folders. "english" can now be "en-gb" or "en-us". Following that, the flag location is changed as well. Here is an example of the new pathing: language/en-gb/en-gb.png. 

Below you will find how you can make your code compatible with OpenCart 2.2.x and previous releases of OpenCart 2.x:

3.1.  Possible fix:

foreach ($data['languages'] as $key => $value) { 
   $data['languages'][$key]['flag_url'] = version_compare(VERSION, '2.2.0.0', "<")
   ? 'view/image/flags/'.$data['languages'][$key]['image']
   : 'language/'.$data['languages'][$key]['code'].'/'.$data['languages'][$key]['code'].'.png"';
}

3.2. Another possible fix, which requires less code:

$flag_url = version_compare(VERSION, '2.2.0.0', "<") ? 'view/image/flags/' . $result['image'] : 'language/' . $result['code'] . '/' . $result['code'] . '.png';

And safely use the variable from that moment on.

4. Revamped image options for your store's template

4.1. Image options are now accessed from Extensions -> Themes. They also have a `code` column value of `theme_default` (for the default theme) in the setting table. Fixing that is relatively easy. Here is one way of doing it:

Possible fix:

private function getImageConfigs($name) {
    if(version_compare(VERSION, '2.2.0.0', '<')) {
        return $this->config->get($name);
    } else {
        if(strpos($name, 'config_image') !== false){
            $name = str_replace('config', '', $name);
            return $this->config->get($this->config->get('config_theme') . $name);
        }
   }    
}

4.2. New variable for getting the maximum products per page.

Just like the images, this setting can now be accessed from Extensions ->Themes. Making this compatible with older versions of OpenCart is also quite easy. Here is one example:

Possible fix:

private function getProductLimit() {
    if(version_compare(VERSION, '2.2.0.0', '<')) {
        return $this->config->get('config_product_limit');
    } else {
       return  $this->config->get($this->config->get('config_theme') . '_product_limit');
    }
}

5. Changed location of some libraries and some functions are deleted

5.1. Some of the libraries are relocated to a different directly. For example, the files affiliate.php, cart.php, currency.php, customer.php, length.php, tax.php, user.php, weight.php are no longer in system/library/. They are moved to system/library/cart/. Keep this in mind if you are using OCMOD or vQmod to modify those files.

5.2 The set() operation is now removed from system/library/cart/currency.php.

6. Code typo in Admin > Customers > Customers page

In the current version of OpenCart 2.2, there is a slight typo error in Admin -> Customers -> Customers, which causes the page to throw an error and not load at all. We are sure that the OpenCart team will fix this issue but until then you can use the following fix:

Possible fix: Modify admin/controller/customer/customer.php and replace line 1105 with this one:

} elseif (($custom_field['type'] == 'text' && !empty($custom_field['validation']) && $custom_field['location'] == 'address') && !filter_var($value['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {

7. The currency formatter now requires a second parameter

Most of the developers know that the method $this->currency->format() had only one required parameter - the value. However, starting from OpenCart 2.2.x, a second parameter is also required - the currency. In most cases, $this->session->data['currency'] would do the trick for a second parameter.

OpenCart 2.2.0.0 Modules

Do you have another trick up your sleeve? Please share it in the comments section. Saved your day? Please share this post with the community.

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time
comments powered by Disqus