Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 27 ยท Getting started

OpenCart 4 and Extension Development

Learn what OpenCart 4 is, how it differs from 3.x, what kinds of extensions you can build, and how its registry and loader fit parts together.

OpenCart is an open-source online store platform written in PHP. It is small compared with the big commerce suites: a fresh install is a few thousand files, it runs on ordinary shared hosting, and its architecture can be understood in a weekend. That is why it has a large marketplace of themes, payment methods, shipping methods and modules, and why there is steady demand for developers who can write them.

This tutorial teaches you to build those extensions for OpenCart 4. You will learn how a request reaches your code, how to read and write data, how to show settings in the admin panel, how to change behaviour without editing core files, and how to package and sell what you build. Every lesson has runnable PHP stand-ins that show the idea in miniature, and says clearly which parts are real OpenCart code and which are our simplified imitation. If you have never built an online shop, skim What Is E-commerce? first, and brush up on classes and objects and namespaces if PHP OOP is new to you.

The real OpenCart cannot run inside this website. Code from OpenCart itself appears in read-only blocks. The TryIt editors run small plain-PHP stand-ins on our PHP runner so you can experiment with the same ideas.

What OpenCart is made of

OpenCart follows an MVC-L pattern: Model, View, Controller, Language. The "L" is real, because every piece of text on screen comes from a language file, never from the template directly.

The application has two front doors:

  • catalog: the storefront that shoppers see (index.php in the web root).
  • admin: the back office where the store owner works (admin/index.php).

Each has its own controllers, models, views and language files. A third folder, system, holds the framework: the registry, loader, database layer, request and response classes, event system, and helper libraries. Extensions add their own code to all three.

TEXT
opencart/
  index.php          storefront entry point
  admin/             back office (controller, model, view, language)
  catalog/           storefront (controller, model, view, language)
  system/            framework: engine, library, helper, config
  extension/         installed extensions live here in 4.x
  image/             product images

A simplified map of the folders. Some are omitted.

Almost everything goes through two objects. The registry is a key-value container holding the shared services (database, config, session, request, response, loader, language, event, url, cache and more). The loader pulls in controllers, models, languages and views on demand. A controller never creates a database connection itself; it asks the registry for the one that already exists.

What changed from 3.x to 4.x

If you find an older tutorial or extension, you need to know what is different. The main changes in the 4.x series are:

  • Namespaces and PSR-style class names. Classes now live in namespaces that start with Opencart\, for example Opencart\Catalog\Controller\Product\Product. In 3.x, class names such as ControllerProductProduct were built by joining words.
  • Twig only. Views are .twig files. The old .tpl templates from the 2.x line are gone, and so is any code that depends on them.
  • No vQmod. vQmod was a community tool for patching files on the fly. OpenCart 4 does not support it. You change core behaviour with events (preferred) or OCMOD (a patch format that remains, as a last resort).
  • Extensions live in their own folder. The 4.x installer unpacks an extension under extension/<vendor>/, with its own admin, catalog and system sub-folders, so core files stay untouched. In 3.x an installer copied files over the core tree.
  • A new route separator. In the 4.0.2 line and later a route calls a method with a dot (extension/acme/module/hello.save). Earlier 4.0 releases used a pipe character. The language also appears in storefront URLs as a language= parameter.
  • Modern front end. The admin and default theme use Bootstrap 5, and many admin forms are saved with Ajax instead of a full page post.

Minor releases of OpenCart 4 (4.0.0, 4.0.1, 4.0.2, 4.1) have each moved some details: the route separator, helper signatures, installer layout. This tutorial describes the common 4.0.2 and later behaviour. When a line of code does not work on your copy, read the matching file in your own installation. It is the final authority.

What an extension can be

OpenCart groups extensions by type. Each type has a contract: a set of methods the core expects.

Type What it does
module A block placed in a layout position (banner, featured products, custom HTML)
payment A way to pay at checkout
shipping A shipping method that returns quotes
total A line in the order totals (tax, coupon, handling fee)
theme Changes how the storefront looks
report, dashboard, fraud, captcha, currency, feed and others Admin widgets and utilities

Beyond these, an extension can use events to react to things that happen (an order is added, a view is about to render) and add its own database tables, admin pages and API routes. The rest of this tutorial walks through each of these in turn.

flowchart LR
    A["Shopper or admin<br/>request"] --> B["index.php"]
    B --> C["Registry<br/>(shared services)"]
    C --> D["Router and Action"]
    D --> E["Controller"]
    E --> F["Model"]
    E --> G["Twig view and language"]
    H["Events"] -.-> D
    H -.-> G

A request is routed to a controller, which uses the model and the view. Events can hook in before and after.

A registry in miniature

The registry idea is easy to copy in plain PHP. The stand-in below is not OpenCart code. OpenCart's Registry class has the same two jobs (store and fetch named objects) and a Controller base class reads from it through magic methods. Run it and notice that the controller never builds the config or the greeting service itself.

PHP A tiny registry and a controller

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

This is the same trick that lets real OpenCart controllers write $this->db, $this->config or $this->load without declaring any of them. It is a form of the service-locator pattern, close cousin to dependency injection. The convenience is real, and so is the cost: a class can use anything in the registry, so dependencies are hidden. Later lessons show how to keep your own logic in plain classes that do not need the registry, which makes it testable.

How names map to classes

The other half of the system is naming. OpenCart turns a route string into a file path and a class name by fixed rules: the segments of the route select the folder and file, and underscores in a file name become capital letters in the class name (weight_shipping becomes WeightShipping). Learning this mapping early saves a lot of confusion, because "class not found" errors usually come from a mismatch between a file path, a namespace and a class name.

PHP From route to class name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The real loader does more: it also builds the file path under extension/acme/catalog/controller/shipping/weight_shipping.php and checks permissions in the admin. You will see the full chain in Routing and Controllers.

Where to go from here

Extensions are most valuable when they match how shops actually work. An order total extension should respect rounding and tax rules; a payment extension has to survive duplicate callbacks. The E-commerce Fundamentals track explains those ideas independent of any platform, and we link to it wherever they matter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the "L" in MVC-L stand for in OpenCart?

    Language files hold all text shown to users, so views never contain hard-coded strings.
  2. 2. Which of these is NOT supported by OpenCart 4 for changing core behaviour?

    vQmod was a community tool and OpenCart 4 does not support it. Events are preferred and OCMOD is the last resort.
  3. 3. What is the registry used for?

    The registry is a container of shared objects. Controllers reach it through magic properties such as $this->db.
  4. 4. Where does the 4.x extension installer place an extension's files?

    Each extension gets its own folder under extension/ so core files are never overwritten.

Summary

  • OpenCart is a PHP store platform with a storefront (catalog), a back office (admin) and a shared framework (system).
  • It uses MVC-L, with a registry for shared services and a loader for parts.
  • Version 4 adds namespaces, Twig-only views, a dedicated extension/ folder and Bootstrap 5, and drops vQmod.
  • Extensions come in types (module, payment, shipping, total, theme and more), each with its own contract.
  • Names map to classes by fixed rules, and version details vary between 4.x minor releases, so check your own copy.

Next: Setting Up a Development Store