No results found

Try a different search query

Popular searches:

Add to Cart

Cart

You have no purchases yet

Browse Marketplace
Original Subtotal:
Product discounts:
Quantity Discount:
Total:
View Cart
Themes and frontend

Custom OpenCart Theme Without Editing Default: Step by Step for 3.0 and 4.1

How to create a custom OpenCart theme so core updates don't overwrite your layout: a separate folder in 3.0, an extension theme with an event in 4.1, custom CSS and packaging.

9 min read OC 3.0 OC 4.1

A custom 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 → theme lets you change the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → appearance without touching core files. Edits to default in 3.0 or catalog/view/template/ in 4.1 will be lost with the first update. In 3.0, a separate folder in catalog/view/theme/ with copies of only the changed templates is enough. In 4.1, the theme is built as an extension that uses an event to substitute its own templates for the base ones.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ. How the core selects the template file in each branch is covered in the article about OpenCart theme structure. Here we cover only the practice: what to create, where to put it, and how to enable it.

Why not simply edit default?

Editing the base templates seems like the fastest option, but it creates three problems that surface later.

  • Updates. Any OpenCart update overwrites core files, and with them your markup. You would have to restore it by comparing files manually.
  • Modules. OCMOD modifications for modules in 3.0 usually target default templates. If you have significantly changed the markup, the search strings of the modifications stop matching, and the modules silently fail to apply.
  • Rollback. When theme templates are kept separately, you can return to the clean look by switching a setting. Once default has been changed, there is no "clean" state anymore.

The theme editor in the admin is not a solution for permanent changes either. It stores code in the database, not in files, so such edits never reach git and override the theme files. It is fine for quickly testing an idea, but not for designing a store.

The correct approach is the same for both branches: base templates remain untouched, and the theme contains only the differences from them. Only the mechanism by which OpenCart finds those differences changes.

How do you create a theme in OpenCart 3.0 without a custom extension?

In 3.0 there is a simplest way, with no PHP code at all: the standard default theme can take templates from another folder. The event/theme handler first looks for the file in the selected folder and takes it from default only if it is missing.

  1. Create the folder catalog/view/theme/mytheme/ with the subfolders template/, stylesheet/, and, if needed, image/.
  2. Copy into mytheme/template/ only the templates you are changing, preserving the path: for example common/header.twig and product/product.twig.
  3. Open "ExtensionsAdditional functionality: a module, a payment or shipping method, a feed, or a theme. They are managed in "Extensions → Extensions." More in the glossary → → Extensions" (Extensions → Extensions), select the "Themes" type, and edit the default theme.
  4. In the "Theme Directory" field, select mytheme. The list is built from all folders in catalog/view/theme/, so the new folder will appear in it immediately.
  5. Make sure the theme status is enabled, and save.
Extensions → Themes: the theme directory is selected in the default theme settings
Extensions → Themes: the theme directory is selected in the default theme settings · OpenCart 3.0.5.1

With this approach, the theme remains "default" from the settings' point of view. The theme's code in config_theme does not change, image sizes and product counts are taken from the same theme_default_* keys, and only the template folder changes. For most stores, this is enough.

Take care of the CSS separately. In the 3.0 header, the path to stylesheet.css is written with the folder name default, so in your copy of common/header.twig replace it with catalog/view/theme/mytheme/stylesheet/stylesheet.css. It is even better to include a separate file with your own styles after the base one: then the base CSS is updated together with the core, and your changes stay separate.

When does 3.0 need a full theme extension?

A folder is enough until you want to sell the theme or give it its own settings. A full 3.0 theme is a theme-type extension with a admin/controller/extension/theme/mytheme.php controllerCode that receives a request and prepares data. More in the glossary →, a language fileA PHP file with interface texts, separate for each language. More in the glossary →, and a form template. After installation, it can be selected in the "Theme" field (Theme) on the "General" tab of store settings, and then config_theme equals mytheme.

There is a trap here that often "breaks" images in new themes. The 3.0 storefront controllers read image sizes and limits as theme_{config_theme}_image_thumb_width, theme_{config_theme}_product_limit, and so on. Therefore, your theme must keep the full set of these keys with its own prefix. If the theme form does not have them, resizing gets empty dimensions, and the product counts in categories change. The easiest way is to take the default theme's form as a model and replace the theme_default_ prefix with theme_mytheme_.

The second mandatory point is the status. The event/theme handler checks theme_{config_theme}_status and, if it is not enabled, stops the storefront with a message that no theme is assigned to the store. Therefore, set the status already in install, and when removing the theme, first switch the store back to default.

The templates of such a theme are located in the same way in catalog/view/theme/mytheme/template/, and missing files are likewise pulled from default. The theme is packaged into an .ocmod.zip archive with the upload/ folder. The path catalog/view/theme/ is included in the allowed directories of the 3.0 installer, as is admin/controller/extension/. Packaging details are in the article how to build ocmod.zip.

How do you make your own theme in OpenCart 4.1?

In 4.1 there are no theme folders, and the storefront core does not switch templates based on the selected theme. The value of config_theme is only stored in the settings. The theme is an extension that, during rendering, redirects the template route to its own files. The minimal structure for the my_theme code is:

  • extension/my_theme/admin/controller/theme/my_theme.php with the settings form and install/uninstall methods;
  • extension/my_theme/admin/language/en-gb/theme/my_theme.php and the form template in admin/view/template/theme/;
  • extension/my_theme/catalog/controller/event/theme.php as the event handler;
  • extension/my_theme/catalog/view/template/ with copies of only the changed templates, for example common/header.twig;
  • extension/my_theme/catalog/view/stylesheet/ for your own styles;
  • install.json in the root of the my_theme.ocmod.zip archive.

When the theme is installed in "Extensions → Extensions → Themes", the core grants permissions to the current group and calls the install method of the theme controller. There we register the event and enable the theme:

Example

namespace Opencart\Admin\Controller\Extension\MyTheme\Theme;

class MyTheme extends \Opencart\System\Engine\Controller {
    public function install(): void {
        $this->load->model('setting/event');

        $this->model_setting_event->addEvent([
            'code'        => 'my_theme',
            'description' => 'Шаблони теми My Theme',
            'trigger'     => 'catalog/view/*/before',
            'action'      => 'extension/my_theme/event/theme',
            'status'      => 1,
            'sort_order'  => 0
        ]);

        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting('theme_my_theme', ['theme_my_theme_status' => 1]);
    }

    public function uninstall(): void {
        $this->load->model('setting/event');
        $this->model_setting_event->deleteEventByCode('my_theme');
    }
}

The handler in the catalog checks that the theme is selected for the store and replaces the route only if the theme has its own file. The view/*/before event in 4.1 passes arguments by reference, so changes to $route and $data are used during rendering. The value returned by the handler is ignored by the core.

Example

namespace Opencart\Catalog\Controller\Extension\MyTheme\Event;

class Theme extends \Opencart\System\Engine\Controller {
    public function index(string &$route, array &$data, string &$code, string &$output): void {
        if ($this->config->get('config_theme') != 'my_theme' || !$this->config->get('theme_my_theme_status')) {
            return;
        }

        // Свій CSS замість базового: шлях задає контролер common/header
        if ($route == 'common/header') {
            $data['stylesheet'] = 'extension/my_theme/catalog/view/stylesheet/stylesheet.css';
        }

        if (is_file(DIR_EXTENSION . 'my_theme/catalog/view/template/' . $route . '.twig')) {
            $route = 'extension/my_theme/' . $route;
        }
    }
}

After installation, select the theme in the "Theme" field on the "General" tab under "System → Settings" (System → Settings). How such events are registered and in what order their handlers run is described in the article about addEvent and handler order.

System → Settings, General tab: the «Theme» field selects the store's active theme
System → Settings, General tab: the "Theme" field selects the store's active theme · OpenCart 4.1.0.4

How do you keep your changes to modules and updates?

A theme that copies templates always lags somewhat behind the core and modules. This lag must be managed, not ignored.

  • Keep the theme in git separately from the core. Then, after an OpenCart update, you can compare the new base templates with the previous version and carry over to your copies only the changes that concern the files you have modified.
  • Check OCMOD in 3.0. If a module changes default/template/product/product.twig, and your theme has its own product/product.twig, the module's change will not appear on the storefront. Either move it into your template, or ask the module author to write the path with a mask for all themes. If a modification did not apply, the article why OCMOD did not apply can help.
  • Do not remove the style and script loops from the header. Modules add their files via $this->document->addStyle() and addScript(), and header.twig outputs them in a loop. Without it, modules will lose their styling.
  • Do not hardcode texts in templates. Strings are taken from the controllers' language files. Hardcoded Ukrainian text will not be translated into the store's other languages.
  • Remember the cache and the theme editor. If a changed file does not show up, check whether there is an entry for this route in the theme editor, and clear the template cache.

Do not transfer class markup between the branches mechanically: the 3.0 storefront uses Bootstrap 3, while 4.1 uses Bootstrap 5. It is easier to maintain a theme for two versions as two separate sets of templates with shared SCSS than to try to make a universal Twig template. For the general strategy across the two branches, there is an article about compatibility with 3.0 and 4.1.

Summary

  • OpenCart 3.0, simple way: a catalog/view/theme/mytheme/ folder with copies of only the changed templates, selected in the "Theme Directory" field of the standard theme.
  • OpenCart 3.0, full theme: a theme-type extension that keeps the status and the full set of theme_{код}_image_* and theme_{код}_product_* keys.
  • OpenCart 4.1: an extension with the catalog/view/*/before event that redirects $route to its own templates and, if needed, changes $data['stylesheet'] in the header.
  • Do not touch base templates, keep the theme editor for experiments, keep the theme in git, and after updates check it against the new base templates.

Next, it is worth looking into Twig syntax and the variables the template receives from the controller: the article about Twig in OpenCart. Other articles on markup are collected in the section templates and frontend.

FAQ

Can one store have different themes for different multi-stores?
Yes, the theme is selected separately in the settings of each store. In 3.0, for the standard theme, the catalog directory is also stored per store, so two stores can take templates from different folders.
Do I need to copy all default templates into my theme?
No. If a file is missing from the theme, OpenCart 3.0 takes it from default, and in 4.1 the example handler overrides only the routes for which the theme has a file. Copy only what you change.
How do I restore the standard look if a theme broke the storefront?
In 3.0, set the default directory back in the standard theme settings. In 4.1, select the base theme in the store settings or disable the status of your theme: the example handler will then override nothing.
Where should I store theme fonts and images in 4.1?
In the extension folder, for example extension/my_theme/catalog/view/, and reference them with paths relative to the site root. Files from image/ in the archive are copied by the installer into the store's shared image directory.
Was this article helpful?
Thanks for your feedback!

Related articles

Didn't find the answer?

Ask the community: OpenCart developers answer on the forum.

Ask on the forum
We use cookies

We use cookies and similar technologies to improve your experience, analyse traffic, and show personalised ads. Read our Cookie Policy for details.