The standard OpenCart module stores its settings in the setting table through the setting/setting model. No custom tables or migrations are needed for this. The admin form submits fields with the module code prefix, editSetting() overwrites them in one batch, and at startup the core loads all rows into $this->config, from which both the admin and the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → read them. Below is how this works in OpenCart 3.0 and 4.1 and where data gets lost in this scheme. Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.
How is the setting table structured?
The table structure is the same in both versions: setting_id, store_id, code, key, value, serialized. The value column is of type text (up to 64 KB) in both branches.
| Column | What it stores | Example |
|---|---|---|
store_id | Store; 0 means the default store | 0 |
code | Settings group used by editSetting() | module_account |
key | Name under which the value will end up in config | module_account_status |
value | String or JSON | 1 |
serialized | Flag indicating that value is a JSON array | 0 |
The code column should be understood as the "owner" of the rows. For store settings, this is config, and for extensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → it consists of the type and code: payment_cod, shipping_flat, module_account. The core relies on this convention. For example, when an extension is uninstalled, it deletes all rows with code = {тип}_{код} for all stores (all store_id) in both branches: in 4.1 this is done by deleteSettingsByCode(), in 3.0 by model_setting_extension. Only settings saved under a different code will remain as garbage in the database.
The key column must be globally unique within a store, although there is no index on it. The core merges all rows into one flat config, so identical keys from two modules will overwrite each other without any error. Hence the rule: every key starts with the owner code, for example module_mymodule_title, not just title. How extension types form these prefixes is explained in the article OpenCart module types.
How does a module save settings via editSetting?
In the admin, the module loads $this and passes the entire form POST to editSetting($code, $data, $store_id). Here is how this method looks in 3.0:
admin/ · OpenCart 3.0.5.1 · lines 19–31
public function editSetting($code, $data, $store_id = 0) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE store_id = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'");
foreach ($data as $key => $value) {
if (substr($key, 0, strlen($code)) == $code) {
if (!is_array($value)) {
$this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "'");
} else {
$this->db->query("INSERT INTO " . DB_PREFIX . "setting SET store_id = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(json_encode($value, true)) . "', serialized = '1'");
}
}
}
}
Three rules follow from this code, which you should know by heart:
- Everything is deleted first. The method removes all rows with this
codeandstore_id, then inserts only what arrived in$data. Saving half of the form means losing the other half. - Keys without the prefix are silently discarded. Only fields whose names start with
$codeare saved. Thetitlefield in the module formmodule_mymodulewill not be saved, and you will see no error. - Arrays become JSON. For an array,
serialized = 1is set, and when reading, the value is decoded back into an array. This is how multilingual fields, product lists, and settings per customer groupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary → are stored.
OpenCart 4.1: the logic is the same, but the code is shorter. The method is typed as editSetting(string $code, array $data, int $store_id = 0): void, deletion is moved into deleteSetting(), and insertion is done in a single query:
admin/ · OpenCart 4.1.0.4 · lines 77–85
public function editSetting(string $code, array $data, int $store_id = 0): void {
$this->deleteSetting($code, $store_id);
foreach ($data as $key => $value) {
if (substr($key, 0, strlen($code)) == $code) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `store_id` = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(!is_array($value) ? $value : json_encode($value)) . "', `serialized` = '" . (bool)is_array($value) . "'");
}
}
}
What does this look like in the module controller?
The easiest way to see it is in the standard account module, which exists in both versions. Its module_account_status key and module_account code clearly show what the prefix should look like.
OpenCart 3.0: the form is submitted via a regular POST to the same route. The index() method, after checking permissions, calls editSetting('module_account', $this, puts the message in the session, and redirects. For the form values, the controllerCode that receives a request and prepares data. More in the glossary → takes data from POST if the form returned with an error, and otherwise from $this.
OpenCart 4.1: the form has the data-oc-toggle="ajax" attributeA product characteristic that does not affect the price: material, weight, power. It is set in "Catalog → Attributes" and displayed on the "Specification" tab. More in the glossary → and is submitted via an AJAX request to a separate save() method at the extension/ route. The method returns JSON with success or error, and the page does not reload. For the status checkbox, the template places a hidden field with value="0" right before the checkbox itself, so a disabled status also gets into the POST.
Example
public function save(): void {
$this->load->language('extension/mymodule/module/mymodule');
$json = [];
if (!$this->user->hasPermission('modify', 'extension/mymodule/module/mymodule')) {
$json['error'] = $this->language->get('error_permission');
}
if (!$json) {
$this->load->model('setting/setting');
$this->model_setting_setting->editSetting('module_mymodule', $this->request->post);
$json['success'] = $this->language->get('text_success');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Field validation, error messages next to each field, and checkbox handling are best moved into a separate settings form topic. For JSON responses and JS-side processing, read the article AJAX responses from controllers. The modify permissions are covered in the article module access permissions.

How do settings get into $this->config?
You do not need to read settings through the model in every controller. At the start of each request, the core selects the setting rows and calls $this, and for rows with serialized = 1 it first runs json_decode($value, true).
flowchart TD
A["Request to index.php"] --> B["startup: select setting rows"]
B --> C{"serialized = 1?"}
C -- "yes" --> D["json_decode into array"]
C -- "no" --> E["value as is"]
D --> F["config->set(key, value)"]
E --> F
F --> G["Controller: config->get('module_..._status')"]
- Storefront, OpenCart 3.0: the
startup/startupcontroller selects rows withstore_id = 0and the current store, sorted bystore_id. This way, store-specific values override the default ones. - Storefront, OpenCart 4.1: the same is done by a separate
startup/settingcontroller through the model methodgetSettings($store_id). - Admin, both versions: only
store_id = 0settings are loaded.
The practical conclusion: in a storefront controller, write $this, not a separate SQL query. The values are already in memory, and an extra database query gives nothing. For more details on file-based configs and their relation to the database, see the article config.php and settings in the database.
Which other setting model methods will be useful?
Besides editSetting(), the model has several methods for point operations. Their names differ between versions, so code ported from 3.0 to 4.1 often fails with an error about a non-existent method.
| Task | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| All keys of a group as an array | getSetting($code, $store_id = 0) | getSetting(string $code, int $store_id = 0): array |
| One value by key | getSettingValue($key, $store_id = 0), returns null if the key does not exist | getValue(string $key, int $store_id = 0): string, returns '' |
| Update one value | editSettingValue($code, $key, $value, $store_id) | editValue($code, $key, $value, $store_id) |
| Delete a group in a store | deleteSetting($code, $store_id = 0) | deleteSetting(string $code, int $store_id = 0) |
| Delete a group in all stores | none | deleteSettingsByCode(string $code) |
One detail is easy to miss: editSettingValue() and editValue() perform UPDATE, not an insert. If a row with such code and key does not exist yet, for example because the module has just been installed, the update will change nothing. For service values such as the last sync date or an API token, first make sure the row is created, for example in the install method via editSetting().
The storefront has its own setting/setting model with the same read methods. OpenCart 3.0: these are getSetting() and getSettingValue(). OpenCart 4.1: getSettings(), getSetting(), and getValue(). The storefront has no write methods, and that is correct: the storefront should not change settings.
What about multi-store setups and large data?
If a module must behave differently in different stores, pass the third argument: editSetting('module_mymodule', $data, $store_id). For the form, this means a store dropdown and loading values through getSetting('module_mymodule', $store_id) rather than $this->config. The admin keeps only the default store in config, so values of other stores cannot be read through config. In the storefront, the core itself overlays the store values on top of the default ones.
The setting table is well suited for configuration but not for data. Signs that a module needs its own table:
- the value grows with the catalog: product bindings, logs, API response cache;
- you need to search or sort by a field inside the value;
- data changes while the storefront is running, not only when the form is saved;
- JSON in
valuereaches hundreds of kilobytes. It gets decoded on every request to every page, because the core loads all settings at once.
The last point matters for performance: each large serialized key slows down every store page. Keep switches, texts, identifiers, and small arrays in setting, and move everything else into the module's tables.
Summary
- Module settings live in
settingunder the code{тип}_{код}, and every key starts with this code. editSetting()first deletes the entire group, then writes only keys with the correct prefix. Always pass it the full form.- Arrays are stored as JSON with
serialized = 1and returned as arrays in$this->config. - In code, read values via
$this, and use the model for other stores and point changes.->config ->get() - In 4.1, methods have been renamed (
getValue,editValue) and typed: check these when porting a module from 3.0.
Next, build a complete module following the step-by-step example of your first module. The rest of the extension topics are collected on the page creating extensions for OpenCart.