Bootstrap in OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → 4.1 is Bootstrap 5.3, while OpenCart 3.0 is built on Bootstrap 3. The differences are not cosmetic: the data-* attributes, the grid, form components, the JS API, and the icon set have all changed. For this reason, module templates from 3.0 cannot be moved to 4.1 by copying them, and the reverse is also true.
The examples are for OpenCart 4.1. In 4.0.x, model triggers and event handling work differently. Below are the actual library versions in the releases and a list of what needs to be changed in the module's templates and scripts.
Which library versions are used in 3.0 and 4.1?
Before rewriting anything, find out what is actually in your store. The library versions in the standard packages are as follows:
| Library | OpenCart 3.0 | OpenCart 4.1 (4.1.0.4) |
|---|---|---|
| Bootstrap on the storefront | Bootstrap 3, catalog/; check the exact version in 3.0.4.1 and 3.0.5.1 in the library file itself | 5.3.8, CSS in catalog/, JS in catalog/ |
| Bootstrap in the admin | Bootstrap 3, CSS compiled from SCSS in admin/ | 5.3.8 |
| Where Bootstrap JS is included | in the header | in the footer (storefront and admin) |
| jQuery | in the header of the storefront and admin; check the version in 3.0.4.1 and 3.0.5.1 in header.twig | 3.7.1 in the header |
| Icons | Font Awesome 4.7.0, fa fa-… classes | Font Awesome Free 7.2.0, fa-solid fa-… classes |
The storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → jQuery directory in 3.0 may contain several versions of the library, including an old jquery-2.1.1.min.js file. Which one the header loads depends on the release and the theme. If the store theme is old and references 2.1.1, modern plugins may not work. Check the page's source HTML rather than just the presence of files.
An important detail for 4.1: Bootstrap 5 does not require jQuery. However, if jQuery is already on the page, Bootstrap 5 registers jQuery wrappers for its components: .modal(), .tooltip(), and .tab(). In OpenCart 4.1, jQuery is loaded in the header, so earlier than bootstrap.bundle.min.js, which means calls like $('#x').modal('show') work. jQuery itself remains in 4.1 because common.js and the core AJAX are written with it. The recommended approach is still the Bootstrap 5 API, for example bootstrap.Modal.getOrCreateInstance(). The old data-toggle and data-dismiss attributes are indeed ignored.
Which attributes and classes need to be replaced?
The most noticeable change is the 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 → prefix. In Bootstrap 5, all data attributes received the bs- prefix, and the old ones are simply ignored without errors in the console. Because of this, a button with data-toggle="modal" does nothing in 4.1.
| Item | OpenCart 3.0 (Bootstrap 3) | OpenCart 4.1 (Bootstrap 5) |
|---|---|---|
| JS attributes | data-toggle, data-target, data-dismiss | data-bs-toggle, data-bs-target, data-bs-dismiss |
| Block with a heading | panel panel-default, panel-heading, panel-body | card, card-header, card-body |
| Alignment | pull-right, pull-left | float-end, float-start |
| Grid for phones | col-xs-6 | col-6 |
| Hide on small screens | hidden-xs | d-none d-sm-block |
| Close button | <button class="close">×</ | <button class="btn-close"></ |
| Default button | btn-default | none; use btn-light or btn-secondary |
| Label | label label-success | badge bg-success |
| Input group add-on | input-group-addon | input-group-text |
Core modules show this difference clearly. The 4.1 storefront filter module is built on card / card-header / card-footer, while similar 3.0 blocks are built on panel. If a module renders its own HTML markup with PHP or JS, for example messages in an AJAX response, these classes must also be changed, not just in Twig.
How has the module form in the admin changed?
The settings form is the first thing rewritten when moving a module, and this is where the most changes occur. The difference is not only in the classes but also in how the form is saved.
OpenCart 3.0
The form has the form-horizontal class, the rows are built on form-group, and the labels have the col-sm-2 control-label class. Saving is done by a regular POST with a page reload, and the controllerCode that receives a request and prepares data. More in the glossary → passes errors to the templateA display file: in 2.x it is .tpl, and in 3.0 and 4.1 it is .twig. More in the glossary → in variables such as error_name and displays them under the field in <div class="text-danger">.
OpenCart 4.1
A form row is row mb-3, a label has the col-sm-2 col-form-label class, a select list is form-select, and the status toggle is form-check form-switch. The form has the data-oc-toggle="ajax" attribute, and the admin common.js submits it via an AJAX request to the address in action. Errors are returned in JSON, and the script highlights the fields itself.
Example
{# extension/my_module/admin/view/template/module/my_module.twig (OpenCart 4.1) #}
<form id="form-module" action="{{ save }}" method="post" data-oc-toggle="ajax">
<div class="row mb-3">
<label for="input-api-key" class="col-sm-2 col-form-label">{{ entry_api_key }}</label>
<div class="col-sm-10">
<input type="text" name="module_my_module_api_key" value="{{ module_my_module_api_key }}" id="input-api-key" class="form-control"/>
<div id="error-api-key" class="invalid-feedback"></div>
</div>
</div>
</form>
The connection between JSON and the field is built by the error key. For $json['error']['api_key'], the script replaces underscores with hyphens, adds the is-invalid class to #input-api-key, and writes the text into #error-api-key. The warning key is displayed as a general message above the form. If the field IDs do not follow this scheme, errors will not be highlighted, even though saving returns them. For how to build the controller for such a form, see the article about the module settings form.
Language tabs
Almost every module form has language tabs, and they are the most likely to break after migration. OpenCart 3.0: list items without classes, links with data-toggle="tab", and the first tab is opened by the $('#language a:first').tab('show') script at the bottom of the template. The language flag is taken from the language/ path.
OpenCart 4.1: items have the nav-item class, links have the nav-link class and the data-bs-toggle="tab" attribute, and the active tab is marked by the active class, added in the loop via loop.first, with no JS at all. The flag comes as a ready-made language.image variable from the controller. If you move a 3.0 template as is, the data-toggle="tab" attribute is ignored, the tabs do not switch, and the language panels either stay hidden or all show at once. The jQuery wrapper .tab() does exist in 4.1, but the inline script in the middle of the page runs before bootstrap.bundle.min.js loaded in the footer, so the wrapper may not exist yet when it is called.
How to rewrite JavaScript for Bootstrap 5?
Code that controls components through jQuery plugins mostly works in 4.1, because Bootstrap 5 registers jQuery wrappers when jQuery is loaded earlier. However, the recommended approach is different: Bootstrap 5 components are created through the classes of the global bootstrap object, and the old data-toggle and data-dismiss attributes must be replaced with data-bs-*.
Example
// OpenCart 3.0 (Bootstrap 3, jQuery-плагіни)
$('#modal-my-module').modal('show');
$('[data-toggle="tooltip"]').tooltip();
// OpenCart 4.1 (Bootstrap 5)
bootstrap.Modal.getOrCreateInstance(document.querySelector('#modal-my-module')).show();
document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function (element) {
bootstrap.Tooltip.getOrCreateInstance(element);
});
The 4.1 core uses exactly this approach: the storefront and admin common.js create tooltips via bootstrap.Tooltip.getOrCreateInstance. The getOrCreateInstance method is safe for repeated calls, which matters for content loaded via AJAX.
The second pitfall is loading order. In 4.1, the bootstrap.bundle.min.js file is included in the footer, so an inline script in the module template that runs mid-page does not yet see the bootstrap object. Wrap such code in $(document).ready() or include your own file with the footer position. In 3.0, Bootstrap is in the header, so this problem does not occur there. For how to include your own files, see the article about including JS and CSS in a module.
Component event names have also changed. Handlers such as shown.bs.modal remain, but they must be attached to the DOM element, and in 4.1 it is more reliable to do this via addEventListener. Events that jQuery subscribes to via .on() also work with Bootstrap 5, but only when jQuery is already on the page.
What about icons and SCSS?
Icons break most quietly: instead of an icon, an empty square or nothing appears. OpenCart 3.0 uses Font Awesome 4.7 with fa fa-pencil classes. OpenCart 4.1 uses Font Awesome Free 7.2 with the fa-solid fa-pencil style classes, and some icon names have been changed in newer Font Awesome versions. For each module icon, check the name in the reference for the Font Awesome version shipped with the store, rather than copying it from 3.0.
In 4.1, Bootstrap CSS and the main styles are built from SCSS. catalog/ contains bootstrap.scss with OpenCart color variables and stylesheet.scss, and the startup/sass controller compiles each *.scss from this directory into .css next to it. Compilation happens if the CSS file does not exist yet or if the SASS cache is disabled in developer settings. It is enabled by default. The SASS cache can be cleared in the developer panel on the admin dashboard.
From this, a practical rule follows: do not edit bootstrap.css and stylesheet.css of the core by hand. After recompilation they will be overwritten from SCSS, and after an OpenCart update the SCSS files themselves will be overwritten. Keep your own styles in a separate file of the module or theme. For how to organize this in a theme, see the article about a custom theme without editing default.
Can both Bootstrap versions be supported with one template?
Technically, you can put both sets of classes in a template, for example pull-right float-end, or the data-toggle and data-bs-toggle attributes together. In practice, this produces a hard-to-read template, and the JS code will still need branching. For admin forms, a shared template makes no sense, because in 3.0 the form is submitted with a regular POST, while in 4.1 it goes via AJAX with a different error scheme.
A working approach is as follows:
- move PHP logic (API clients, calculations, database work) into a shared library that does not depend on the version;
- maintain admin and storefront templates separately for each branch;
- write your own styles with your own module-prefixed classes, for example
.my-module-card, and rely on Bootstrap components minimally, so the CSS transfers with almost no changes; - write JS with the plain DOM API and do not depend on Bootstrap's jQuery plugins, so the code works in both branches.
The general scheme of a shared core and two thin packages is described in the article about module compatibility with 3.0 and 4.1.
Summary
- OpenCart 3.0: Bootstrap 3, Font Awesome 4.7, Bootstrap JS in the header, components controlled by jQuery plugins.
- OpenCart 4.1: Bootstrap 5.3.8, Font Awesome Free 7.2, Bootstrap JS in the footer, components controlled via
bootstrap.Modal,bootstrap.Tooltip, and so on. - The
data-*attributes received thebs-prefix, panel became card,pull-*becamefloat-*, andcol-xs-*becamecol-*. - The 4.1 admin form is saved via
data-oc-toggle="ajax", and field IDs must match the error keys in the JSON. - The core CSS in 4.1 is generated from SCSS, so keep your own styles separate.
Before migrating a module, go through the templates and JS and search for old classes and attributes: this will give you a real picture of the workload. Other articles on markup are collected in the section templates and frontend.