Opencart tutorial – how to code single and multi-instance module?

In this Opencart tutorial, we are showing you the difference between the coding structure of a single instance Opencart module and an Opencart multi-instance module both in the admin section and the catalog section. In a single instance module, you install and can create only one setting and can use it in multiple places, but everything will be the same. In the multi-instance module, you install and can create multiple modules and can add different settings and can use them in multiple places with different content.

Single Instance Opencart moduleMulti-Instance Opencart module
After installation, it will create only one moduleAfter installation, it can create many modules as many as needed
Single vs Multi instance opencart module
Example of core available only one instance Opencart module are: Account module, Category module, Information module, etcThe name of the form field can be any
Admin section code change
$this->load->model(‘setting/setting’);$this->load->model(‘setting/module’);
$this->model_setting_setting->editSetting(‘module_login’, $this->request->post);if (!isset($this->request->get[‘module_id’])) {
his->model_setting_module->addModule(‘bestseller’, $this->request->post);
} else {
$this->model_setting_module->editModule($this->request->get[‘module_id’], $this->request->post);
}
Name of the form field should start with the extension initials, for the module, it is like name=”module_*****”, for the shipping extension, it is like name=”shipping_***”, for payment extension name=”payment_***” etc.Data are saved in the oc_module database table
Some other extra codes for multi-instance opencart module are:
– Form action link needs to be if-else as per module id
– Last Breadcrumbs link need to link to module id link
Extra code:
if (isset($this->request->get[‘module_id’]) && ($this->request->server[‘REQUEST_METHOD’] != ‘POST’)) {
    $module_info = $this->model_setting_module->getModule($this->request->get[‘module_id’]);
}
In the oc_layout database table, the code dot module id is stored in the code column
Catalog section code change
The code difference in the controller is at the index method.
public function index() {
public function index($setting) {
Data are saved in the oc_setting database tableIn the oc_layout database table, the cod will be stored in the code column
Opencart database layout
Some other extra codes for multi-instance opencart module are:
– Form action link needs to be if-else as per module id
– Last Breadcrumbs link needs to link to the module id link
Extra code:
if (isset($this->request->get[‘module_id’]) && ($this->request->server[‘REQUEST_METHOD’] != ‘POST’)) {
    $module_info = $this->model_setting_module->getModule($this->request->get[‘module_id’]);
}

These are the differences between a single-instance module and a multi-instance module of Opencart. If you are looking for an Opencart module development tutorial, here are the two videos and four posts that will help you understand the code and flow of Opencart modules.

Please let us know if you have any questions or suggestions, please subscribe to our YouTube Channel for Opencart video tutorials and you can see more at Opencart tutorial category. You can also find us on Twitter and Facebook. Enjoy!

Previous articleHow to add reports in the admin dashboard of Opencart 4?
Next articleSmart Selling Strategies: Programming, E-commerce, and the Art of Budgeting

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here