Repurposing a Module - xerocrypt/opencart GitHub Wiki

Deals with the modification and installation of a duplicated module. This is the best way to get started with OpenCart module development.

This is specific to version 3.0.2 of OpenCart, and assumes you have access to the server's filesystem (e.g. through cPanel).

What I am going to do here is copy the code for an HTML editor module. Since there is one module installed that enables the insertion of arbitrary HTML, and cannot be repurposed through the dashboard.

Essentially all I'm going to do here is copy all the files for the HTML content module, do some renaming and repackage it as an additional module that can be repurposed in the opencart dashboard.

The original module is simply called 'HTML'. The module duplicate I'll call 'GenericHTML'.

1. Copy Existing Module File System

Files needed:

  • /admin/controller/extension/module/[filename].php

  • /admin/language/en-gb/extension/module/[filename].php

  • /admin/view/template/extension/module/[filename].twig

  • /catalog/controller/extension/module/[filename].php

  • /catalog/view/theme/default/template/extension/module/[filename].twig

You want to copy the files into the same file structure within the clone directory.

2. Basics of Operation

As this is a separate module, we'll need to add a controller for it. As it doesn't do anything much, beyond display the HTML defined by the user, we only need the generic PHP functions.

It is probably a good idea to explain something about what these files do. As OpenCart is based on the MVC code pattern. A client's browser sends a request to the server, and the request is routed to a controller within /admin/controller or /catalog/controller, depending on whether the application is being accessed through the home page or dashboard.

This controller would look something like:

class ControllerExtensionModuleGenericModule extends Controller { public function index($setting) { [...] Code here [...]
    return $this->load->view('extension/module/myview', $data);
}

}

The function executes whatever code, and returns a 'Web page' defined by /extension/module/myview along with whatever content is generated by the controller.

The controller and view in /admin determine what the site admin sees, and the controller-view in /catalog determine what's displayed to others visiting the site.

3. Modifying the Cloned Files

The important thing to change in /admin/controller/extension/module/[filename].php and /catalog/controller/extension/module/[filename].php are the names for the controller classes,

Useful Links

Building your first OpenCart 3 extension

OpenCart: Developing Modules

OpenCart: How to create/duplicate a module?

OpenCart 3 custom module development tutorial - Hello World module

Stack Overflow: Developing custom module in OpenCart 3

⚠️ **GitHub.com Fallback** ⚠️