How To Create An OpenCart Extension

Creating an OpenCart 2.x extension is a fairly simple process and there are many different resources available out there to help you with this.

Today I would like to share the MultiMerch way of doing this. This is how we create our OpenCart extensions and MultiMerch addons.

I'll try to make it as simple and easy to understand as possible.

Displaying a custom page in OpenCart is fairly easy, but in most cases you'll need more than that. We usually need our OpenCart extensions to have at least the following parts (in addition to the main logic):

  • a simple way to install and remove them
  • configuration/settings (and a simple interface to change them)
  • database tables to store and retrieve data (and a way to create and delete them without asking the user to run SQL scripts manually)
  • support for multiple languages (OpenCart has quite a multilingual user base)

Let's create a simple extension skeleton that will allow us to do all of this. Today we'll cover the admin-area part of the extension without settings (I'll talk about them in Part 2).

You most likely already know that OpenCart is based on a fairly straight forward MVC architecture plus a set of language files. The Models store the database procedures, the Views hold the HTML code of pages and Controllers fetch the data using Models and use it to render Views.

In addition to this, OpenCart's main code is split in two separate parts (admin/ and catalog/, each containing an MVC structure) along with common files and libraries under system/.

Today, we only need the admin/ part. OpenCart usually stores its extension files under the module/ subfolder inside each of the MVCL folders. Let's stick to this and create the following folder structure in our workspace:

OpenCart Extension Folder Structure

Great! This structure will hold our models, views, controllers and languages.

Note that we've placed our structure inside a separate upload/ folder with a readme.txt file outside of it. This isn't mandatory, but it makes it easy to separate what needs to be uploaded to OpenCart installation from everything else.

Now let's start adding files.

Most users expect the newly uploaded extensions to appear under Admin > Extensions > Modules. OpenCart will display our new extension on this page automatically, but it needs two files to be able to do it: language file (to display the title) and controller file.

Let's start with the language file.

Create a new file called multimerch_skeleton.php under admin/language/english/module/ and add the following code to it:

Now create a file named multimerch_skeleton.php under admin/controller/module/ consisting of this empty class:

Make sure that file names and class names match. OpenCart uses this to determine what to load and where to find it.

You can now upload the contents of the upload/ folder (not the folder itself!) over to your OpenCart setup root and look into Admin > Extensions > Modules:

opencart extension skeleton admin

Neat, our Skeleton is there! It doesn't do much more than that, but it's already visible in the admin area and can be installed and uninstalled through OpenCart's own system.

Now, what if we also need a table in the database for our extension? Let's make OpenCart helps us do it!

First, we need the following multimerch_skeleton.php model file inside admin/model/module/:

Here we have a simple database table with a single field that we want created and removed when Skeleton is installed/uninstalled. The DB_PREFIX constant holds the table prefix that was selected during OpenCart installation. Don't forget it when working with the database!

Now we need to tell OpenCart to call these functions. Let's add the following code to the multimerch_skeleton.php controller file inside our ControllerModuleMultimerchSkeleton class:

These two controller functions are called by OpenCart automatically when you install and uninstall the module through Admin > Extensions > Modules.

Now we can upload our files and test them out!

Let's try installing MultiMerch Skeleton via Admin > Extensions > Modules to see what happens.

opencart extension successfully installed

opencart extension skeleton table

Awesome! Now our Skeleton extension is installed and the table is there. It will be removed if Skeleton is uninstalled via Admin > Extensions > Modules.

That's it for today, hopefully this helped you learn something new about OpenCart extensions.

Meanwhile, let me know in the comments if I've forgotten something or if you have any questions!

Need an OpenCart extension? Hire us!

We offer a wide range of professional OpenCart & MultiMerch services. Find out more!

12 replies
  1. Rike Deodato
    Rike Deodato says:

    Great article, waiting part 2. Is there in open cart a hook system like in drupal? I started studying opencart now and that part is not clear to me.

    Reply
  2. JoeandJosi Stock
    JoeandJosi Stock says:

    Hey, I never comment. Ever. However I had to this time. This tutorial is extremely easy to understand. Awesome Job and I cant wait to continue with the next part. There are very few really good tutorials on Opencart and module creation, especially for the latest version. So, thank you for this VERY GOOD tutorial. - Regards - Joseph

    Reply
  3. Kris Randall
    Kris Randall says:

    I agree that this is a great tutorial, and am very eager to know when part 2 is planned ... hmmm ... part 1 was made a year ago (and 2 days) ... I'm thinking maybe there is not going to be a part 2 ?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.