Basic usage


This is a very simple example of how to create an extension with KaMod technology. Let's add a new menu element to the store back-end. For example, we want to add a direct link to the 'Shipping list' page at the very bottom.

We assume that you already installed "Ka Extensions" library for Opencart 4.

HINT: Before diving to development with kamod you should enable rebuilding kamod cache on every page load. It will drastically speed up your development process. Open system\startup.php file
and define a new constant there:

define('KAMOD_DEBUG', 1);

Now we are ready to make our first Opencart 4 extension with kamod.

1. We create a directory for our extension as usual and create 'kamod' directory inside it.
\extension\shipping_menu\kamod

and we put active.kamod file to the extension root directory. That file is just a marker to activate the extension kamod files and its contents is not important. The file can be empty.

Next, we need to override the file where the menu structure is prepared. It is
\admin-secret\controller\common\column_left.php

Inside kamod directory we replicate the original file path and place our file at this path:
\extension\shipping_menu\kamod\admin\controller\common\column_left.php

NOTE: we use the 'admin' directory name here instead of the real one.

inside that file we create our own class under our own namespace. And we declare the function of the parent class with the same parameters. Now, our function will be called before the parent function! We are free to disable or replace the menu at all.

namespace extension\shipping_menu\common;
class ControllerColumnLeft extends \Opencart\Admin\Controller\Common\ColumnLeft {
public function index(): string { // here we can call the parent function return parent::index(); } }
IMPORTANT: do not split class declaration to several lines and specify the full parent class including the namespace. Kamod parser cannot handle complex extension constructions in class declaration.

But we need to modify the output of the parent function. By default it returns an already generated menu as a text and therefore we need to change the menu structure before generation. With kamod and some Ka Extensions magic it is possible!
For this purpose Ka Extensions offers several helpful functions collected in one trait. That trait should be included to your controller class and after that we can call those new functions to get the data array with the template name from the parent class.

Instead of many words there is a complete code below with comments.

namespace extension\shipping_menu\common;

// we extend the standard Opencart menu class
class ControllerColumnLeft extends \Opencart\Admin\Controller\Common\ColumnLeft { // this trait adds functions for disabling render of the parent class // and getting the render information from the parent class call use \extension\ka_extensions\TraitController;
public function index(): string { // disable template generation $this->disableRender(); // let the parent function do all necessary preparations
parent::index(); // enable template generation
$this->enableRender();
// here we get the data array() and the template name // that were passed to the last view call by the parent function //
$data = $this->getRenderData(); $template = $this->getRenderTemplate();
// check the data (just in case) //
if (empty($data)) {
return '';
}
//
// prepare our menu item
// // take the menu name from an existing language file
$this->load->language('extension/shipping');
// create the menu similar to default ones
$item = [
'id' => 'menu-shipping-list',
'icon' => 'fas fa-shipping-fast',
'name' => $this->language->get('heading_title'),
'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=shipping'),
'children' => []
];
// put our new menu item to the existing menu array
array_push($data['menus'], $item);
// ask Opencart to render the template this time
return $this->load->view($template, $data);
} }

If you wish to install this extension as a regular Opencart extension, you will need to pack it into an archive and add a install.json file inside.

A working module with that example can be downloaded at this URL:
https://www.ka-station.com/samples/kamod/shipping_menu.ocmod.zip

Please Wait!

Please wait... it will take a second!