How to Develop Opencart Modules

How to Develop Opencart Modules
 

What Are OpenCart Modules?

OpenCart modules are analogous to add-ons, plugins, or extensions in other content management systems. It's through modules that OpenCart gives us the ability to extend its functionality without having to edit the application's files.

As with many other content management systems, it's generally considered to be a best practice to extend functionality of the core application through the provided APIs and OpenCart is no different. Modules allow us to introduce, remove, or modify functionality of the core application that's done in a compartmentalized and maintainable way.

OpenCart has its own Extension Market where a large number of extensions are already available. 

To take things a step further, we're going to look at creating a custom module for OpenCart.

 

Our First Module

To get started, perform the following:

1.   Create an Controller to Admin

Path: admin/controller/extension/module/popular.php .

2.   Create a Language File to Admin

Path: admin/language/en-gb/extension/module/popular.php .

3.   Create a View to Admin Path: admin/view/template/extension/module/popular.twig .

1. The Language File

As discussed in our previous articles, the language file contains the static text what should be displayed in our view file. For the popular.php language file, the following variables contain the possible text fields what we require to display in our module:



1. The Controller

Open the "popular" controller file that we just created and add the class class ControllerExtensionModulePopular {}following the Class Naming Convention. Next, place the following code inside the class






Step 2: Validation Method

As we tried to validate data on save in default function. So here comes the validation method.


3. View File

As previously done in controller, you have to create some HTML for the view. For that, we'll do the following:


Step 4: At this point, we're done preparing our first popular module. At this point, it's time to check whether our module is working or not. 

To do that, login to dashboard and go to the Extensions Tab->Extensions page where you'll see option Choose the extension type and select modules from the dropdown.


There will also be "popular" listed with an "Uninstalled" State, click on "Install" and try editing the module and you'll see a screen something like this:


Conclusion

In this article, we tried to build a basic OpenCart Module using MVC. It's easy to manipulate OpenCart Modules if you're familiar with the core concepts of MVC. This article gives just a basic idea how to develop a simple module following some simple steps.

Posted by Hasan

Nurul Hasan - Project Manager - N2R TECHNOLOGIES