How To Create An OpenCart Extension: Part 1

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.

This is the first part of the tutorial – 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. In Part 2, we'll cover configuration, settings page and a few other important things.

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

Martin Boze

Martin Boze

About the authorMartin Boze is the Founder of MultiMerch Marketplace. I also love skydiving, motorcycles and space. And other cool things.
Martin Boze

Latest posts by Martin Boze (see all)

  • Jitesh

    Thank you for the amazing tutorial. Awaiting for the next part. :)

  • One of the best tutorials. Is there any plan for Part 2?

    • Martin Boze

      Yes, Part 2 is in progress :)

      • Zohaib Hassan

        too slow progress :P

      • sheo

        Hello when next part is coming .. ??

  • Athos Vinicius Pereira

    Part 2? :D

  • Rike Deodato

    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.

  • tag raynor

    it's really easy to understand . thanks for that .

  • Artem Travlo

    Thanks))

  • JoeandJosi Stock

    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

  • Kris Randall

    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 ?