Compatibility: OpenCart 2.3x
This guide will make more sense to people who are familiar with PHP, OOP and MVC. To beginners, it should serve as a good starting point
You have the MVC(L) structure. According to someone on StackOverflow though
Well .. actually, OpenCart does not use MVC pattern. They have a bastardized version of Rails-like architecture. It doesn’t have views (only dumb templates), it doesn’t have model layer (only table gateways) and all of the logic is dumped in something they call “controllers”.
The Models store the database procedures, the Views hold the HTML code of pages and Controllers fetch the data using Models and use it to render Views In addition to this, OpenCart’s main code is split in two separate parts (admin/ and catalog/, each containing an MVC structure) along with common files and libraries under system/.
If you don’t know what MVC(L) is, it’s
OpenCart MVC(L) folders
.
├── admin
│ ├── controller
│ ├── language
│ ├── model
│ └── view
├── catalog
│ ├── controller
│ ├── language
│ ├── model
│ └── view
Here’s what the folder structure for a module called foo
will look like
Module Structure using MVC(L)
.
└── admin
├── controller
│ └── extension
│ └── module
│ └── foo.php
├── language
│ └── en-gb
│ └── extension
│ └── module
│ └── foo.php
└── view
└── template
└── extension
└── module
└── foo.tpl
Here’s a bash script to create all the files
1#!/bin/bash
2
3# RUN: bash script.sh moduleName
4
5# The first argument provided will be takes as module name
6## This will be the filename for all files created
7moduleName=$1
8
9# Admin folders
10## On the site these folders already exist
11## But you'll create them again in order to upload your files
12mkdir -p admin/controller/extension/module # Controller
13mkdir -p admin/language/en-gb/extension/module # Language
14mkdir -p admin/view/template/extension/module # View
15
16# Admin files
17touch admin/controller/extension/module/${moduleName}.php # Controller
18touch admin/language/en-gb/extension/module/${moduleName}.php # Language
19touch admin/view/template/extension/module/${moduleName}.tpl # View
1?route=folder/file/method
2
3?route=folder/subFolder/file/method
4
5?route=folder/file
OpenCart uses the route
in the query string parameter to determine what to load, and this is how you can find which files you need to edit for which page. If you know how the routes work, you will never have to ask the which file do i edit to make changes to X question. (fyi: it’s a very common question on OpenCart forums)
Usually you have only two parts for the route ?route=folder/file
. If no method is specified, then it means you ran the index()
function
index()
method.common
folders)value
attribute of the radio input for that shipping/payment)<body>
tag. For example, if you see a class product-product-40
you are on ?route=product/product&product_id=40
)admin/language
or catalog/language
english/english.php
(en-gb/en-gb.php
in OpenCart 2.3x onwards) are automatically loaded and available to use without the $this->language->load
methodThe four lines of code that’ll take care of the all that are as follows:
1// Define language text (language.php)
2$_['heading_title'] = 'My Heading';
3
4// Load language (controller.php)
5$this->language->load('foo/bar');
6
7// Assign language text to variables (controller.php)
8$data['heading_title'] = $this->language->get('heading_title');
9
10// $data is passed to the template via $this->response->setOutput()
11
12// Use that variable in the template (template.tpl)
13<h1><?php echo $heading_title; ?></h1>
Language folders
admin->language->en-gb->extension->module // OpenCart 2.3x and above
admin->language->en-gb->module // OpenCart 2.2x
admin->language->english->module // OpenCart 2.1x and below
Define language variables in your language file using a special variable $_
which is an array of keys and text values. $_
is the name of the array
1// admin/language/foo/bar.php
2
3// We're assigning the languages to the indexes of an array.
4// $_ is the name of the array
5$_['heading_title'] = 'My Heading';
Load the language file in the controller
1// admin/controller/foo/bar.php
2// $this->language->load('foo/bar'); // OC 1.5x
3$this->load->language('foo/bar'); // OC 2x and above
Once you have the language loaded, you can use the language library function get
to retrieve specific language texts, and assign them to variables
1// admin/controller/foo/bar.php
2
3// assign language texts to variables
4$some_variable = $this->language->get('heading_title');
It’s kind of redundant though. All those language values you have already defined in language.php
, you have to assign them all AGAIN in controller.php
in order to use them in template.tpl
. This is so not DRY. One solution, as recommended here, is as follows
1$data = $this->load->language('catalog/product');
This one line above automatically assigns every thing in the language file to the $data
object.
Usually, all values are saved on the $data
object, which is then passed on to the template via the setOutput()
method
1// admin/controller/foo/bar.php
2$data['heading_title'] = $this->language->get('heading_title');
3
4$this->response->setOutput($this->load->view('foo/bar', $data)); // the $data object has everything that gets sent to the template
You can now use all the language variables you defined in controller and passed to template with their keys. e.g.
1// admin/catalog/foo/bar.php
2<h1><?php echo $heading_title; ?></h1>
Permissions are only allowed in modules. To grant permissions by default (instead of going to User Groups and editing it manually), you need an install()
function in your controller. Place the following code inside your install()
method
1public function install() { // this will be run when you install the module under EXTENSIONS > MODULES in the admin area
2 $this->load->model('user/user_group');
3 $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'foo/bar');
4 $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'foo/bar'); // you need the 'modify' permission in order to install/uninstall/modify the module
5 }
6}
Show an error if the user doesn’t have permissions to edit your module
1// admin/controller/foo/bar.php
2protected function validate() {
3 if (!$this->user->hasPermission('modify', 'foo/bar')) {
4 $this->error['warning'] = $this->language->get('error_permission');
5 }
6
7 return !$this->error;
8}
Generally, the settings for a module are saved in the settings
(usually oc_settings
) table in the database. The queries are defined in admin/model/settings/settings.php
catalog/view/javascript
foldercatalog/view/javascript/common.js
has common code used in your frontend. This contains AJAX calls, event handlers etc.1// Scripts
2// addScript($href, $postion = 'header')
3$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');