If you are a module developer or want to customize an existing module, you'll be surprised that you can't check the module id in the code. By default, OpenCart doesn't pass module_id to module settings, and this is particularly frustrating for multi-modules that allow creating multiple instances of a module with different settings.
Why module_id is needed
The module identifier is a unique number for each module in the system. It's important in many usage scenarios. When you create multiple instances of one module with different settings, module_id allows you to distinguish between them. When interacting with the database, module_id enables creating queries that work only with data from a specific module instance. This is important for modules that store user settings or work results or for caching. Or simply for checking in code if you need to add something only for a specific particular module, not for all its adjacent copies.
By default, OpenCart passes only settings from the setting field of the database to the module controller. When the system calls a module, it unpacks the JSON with settings but loses information about the module identifier itself. This means that inside the module controller, the developer doesn't have direct access to module_id without additional code manipulations.
Solution through OCMOD
The simplest way to solve this problem is to create an OCMOD modification that automatically adds module_id to module settings. This approach practically doesn't violate the standard OpenCart architecture and ensures compatibility with all existing modules.
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Fix: Add module_id to setting</name>
<code>fix-module_id</code>
<version>1.0.0</version>
<author><![CDATA[<kbd>opencartbot</kbd>]]></author>
<link>https://opencartbot.com/en/</link>
<file path="catalog/model/setting/module.php" error="skip">
<operation error="skip">
<search><![CDATA[
return json_decode($query->row['setting'], true);
]]></search>
<add position="replace"><![CDATA[
$settings = json_decode($query->row['setting'], true);
$settings['module_id'] = $module_id;
return $settings;
]]></add>
</operation>
</file>
</modification>
Download ready OCMOD modification
The modification changes the getModule method in the catalog/model/setting/module.php file. Instead of simply returning parsed JSON settings, the system now adds the module_id field with the corresponding value to this array.
This approach guarantees that each module automatically gets access to its unique identifier without the need for additional changes in controller code.
Installing and activating the modification
After downloading, go to Extensions → Modifications section and click the Refresh button to apply changes. The system will automatically apply the modification to the required file.
The modification is fully compatible with existing modules and doesn't require changes to their code. All modules that don't use module_id continue to work as usual. The modifier has no impact on system performance since it only adds one value assignment to the existing process. At the same time, it's universal and works with all types of modules without exceptions.
Example of using module_id in module code
After installing the modifier, you can easily use module_id in your modules. In the module controller, the identifier becomes available through the settings array:
class ControllerExtensionModuleYourModule extends Controller {
public function index($setting) {
$module_id = isset($setting['module_id']) ? $setting['module_id'] : 0;
// Using for caching
$cache_key = 'your_module.' . $module_id;
$data = $this->cache->get($cache_key);
// For database operations
$this->db->query("UPDATE custom_table SET data = 'value'
WHERE module_id = '" . (int)$module_id . "'");
// For logging
$this->log->write('Module ' . $module_id . ' executed successfully');
}
}
If you need to pass module_id to the module template, add this line to the module controller:
$data['module_id'] = isset($setting['module_id']) ? $setting['module_id'] : 0;
As you can see, getting the module id is very simple and we've shown you one of the methods, the simplest and fastest way to do it. This will allow you to expand your system capabilities and customize your modules to fit your needs.