What is Opencart?
Opencart is an online store management system built using PHP programming language and MySql database. It is a framework and system with many basic shopping store functionalities already integrated. Opencart is widely used thanks to it’s developer-friendly environment, easy setup and huge online modules store where many of the modules are completely free to install. Modules/Plugins are an organized collection of files/code that can be integrated in the existing system to extend it’s functionality.
You can download, install and optionally refactor Opencart modules and adapt them for your purpose. However, you can also develop your own modules from scratch, and extend Opencart functionalities to suit your project’s needs. In this article we will get you through the basic Opencart code structure and describe the process of creating a simple Opencart module. This should be enough for you as a developer to gain a ground understanding of Opencart and continue exploring more about developing your own web shops and apps.
Opencart Basic Code Structure
First let’s see how Opencart code is organized, so we have a nice understanding of the environment before we dig into the basics of module development.
To start, please go to the Opencart official website and download the latest version. It should be extracted in the folder accessible by your server, either localhost xampp/Apache or remote.
Basic Opencart installation contains several folder and files, which I will not cover in detail but will give you a slight introduction. To work on the OC and extend it’s functionality, you need to be familiar with PHP programming in general. Of course, if you are familiar with PHP, you know that the entry point of the app is index.php. From that file you can trace execution of the app. Core of Opencart framework is a system folder which contains many core classes with native OC functionalities that you will also use during development and customization.
After system folder, which is a core OC folder and should never be directly changed by the user-developer (modifications are used for this and I will cover them later), two other important folders are admin and catalog. These folders contain code which is responsible for the admin and front (actual website for visitors) part of the app. Inside both of these folders, according to MVC design programming principle which is adopted in the OC, files are organized in the controller, model, view folders + language folder which contains language customizations.
This is just the basic structure, and here’s a quick tip: index.php is the opening gate of the app, in our OC project at the end of the URL you will usually see something like index.php?route=product/product. This means that we are calling the index method of the product.php controller which resides in the product folder of the catalog’s controller folder. If we have index.php?route=product/product/send , we are calling send method from the same file. Index is just the default method that is being called when no actual method name is passed. The same goes for admin part, calling /admin/index.php?route=common/dashboard means that we are calling index method of the dashboard.php class file that resides in the common folder of the admin directory.
How Modules are organized?
Modules are nothing more than a group of logically connected PHP files which are integrated in OC to provide some specific functionality. They also follow MVC pattern and contain a controller (logic part), optional model (usually for DB queries), view (template) and language file.
In the current version 2.3, controller of the module resides in the controller/extension/module/ folder. This same path is followed in the model, language and view directories as well, so be sure to place those files in the right place. This principle is the same for the admin and catalog part.
At Blue Factory, we are working on OC projects from scratch and make many custom modules with interesting functionalities. For example recently we made an Instagram fetching module, which uses Instagram API to fetch latest Instagram images from the specific Instagram account that we define inside the admin settings of the module (admin part of the module is responsible for settings). Then images are inserted in the footer of the OC website using something that is called events in the OC development. Defining an event is a great functionality of OC which allows us to hook some new code/event to specific moments of execution – for example, loading of the footer, we can intercept that loading and insert new data in it! That’s the case for all controllers, views etc. we can intercept them all while loading.
There are many tutorials online that show basic module development process.
Slight Google search will give you access to many of them, and I encourage you to do so. If you have any questions or need help in customization or development, just contact us at Blue Factory.
A developed module can be packed inside the special types of files that OC recognizes and installs them as extensions inside the admin control panel, through something that is called OCMOD installation system. Important thing to remember is that as of Opencart version 2.3, these zip files must have extension .ocmod.zip to be recognized by the system, and one folder called upload which has folders and files inside that must resemble the exact paths that they will reside in the code of the project. Here is an example of the zipped module:
upload/ admin/ /controllers/extensions/module/module_name.php /language/en-gb/extension/module/module_name.php /view/template/extension/module/module.name. catalog/ /controllers/extensions/module/module_name.php /language/en-gb/extension/module/module_name.php /view/template/extension/module/module.name.
This structure can also contain other files such are readme file or SQL file with commands that should be executed on install.
In OC there is also a concept of modifications, which are specially written in XML markup language and contain instructors to OC to replace-change the code at the exact places. Modifications are also installed through the extensions installer in Admin, which allows us to enable / disable them without actual change to the code (which is considered bad practice when it comes to system and admin folder).
Useful tip: How to log stuff while developing?
One thing that was very useful to me while starting to develop with OC is that you can log data into the error.log which resided in system/storage/logs/ folder. Beside using PHP debugger such is XDebug, this easy way of logging data or variables from your code was very useful to me in the past.
To log data somewhere in your code, assuming that you are in the class which extends basic Opencart Controller which contains write() method, you should call that method with:
$this->log->write($variable); // $variable or just some “message”
Variable $variable will be written in the system/storage/logs/error.log according to the execution of the portion of the code where it resides.
I hope that this introduction give you a better understanding of OC and how it can help you build a great online shopping system that can be further extended and improved up to your needs using already mentioned modules, modifications, controlled customization of existing code etc.
Thanks for reading and happy coding.