Hi me Rupak Nepali again, today I am going to cover OpenCart OCMOD documentation and tutorial for the developer and show how to start coding in OCMOD to change the files and extend functionalities. How are OCMOD code flows described here?
OCMOD is based on Qphoria’s VqMOD system.
Opencart 3 install uninstall method in OCMOD
The detailed documentation of
Both have the same functionalities, you have to install VqMOD in OpenCart 3 and OpenCart 2 but OCMOD is by default in OpenCart 3 and you can easily upload from the admin sections.
The main feature differences for the programmers are:
Both
Replace, Before, After, Regex, Offset, Limit,
Missing in OCMOD which had in VqMOD are:
Attributes top,
The file and folder structure to create OCMOD in OpenCart 3 are like below:
- The File structure of OCMOD compressed zip ocmod.zip files may look like below but can differ as per the functionalities of the module
- For an OCMOD file to be uploaded the file extension must be either .ocmod.zip or .ocmod.xml.
File Structure
- Example file structure for OCMOD compressed files.
- upload
- install.sql
- install.php
- install.xml
upload
- All files under this directory will be uploaded to the directory of your OpenCart installation.
install.sql
- Add any create, drop, insert, and update SQL queries here. Make sure each query ends with the following;
install.php
- If the modification requires any custom PHP code.
install.xml
- The XML modification file. You can view the documentation for this system below.
https://github.com/opencart/opencart/wiki/Modification-System
Demo to install the OCMOD
- Let’s install ShowModuleLinkAtLeftMenu module which is developed by webocreation.com
- Here you can see only the install.xml file as it does not upload any files The file changes the core files.
- Download the opencart module Show Module link at Left Menu for free
- Admin >> Extensions >> Extension Installer (upload ShowModuleLinkAtLeftMenu.ocmod.zip here) then to modification and click refresh.
Let’s see XML example of the module install.xml
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Show Module Link At Left Menu</name>
<version>3.0</version>
<author>Rupak Nepali</author>
<link>https://webocreation.com</link>
<code>webocreation_show_module_link_at_left_menu</code>
<description>Show module link at left menu</description>
<file path="admin/controller/common/column_left.php">
<operation>
<search><![CDATA[ if ($this->user->hasPermission('access', 'marketplace/extension')) { ]]></search>
<add position="after"><![CDATA[
$marketplace[] = array(
'name' => "Modules",
'href' => $this->url->link('marketplace/extension&type=module', 'user_token=' . $this->session->data['user_token'], true),
'children' => array()
);
]]></add>
</operation>
</file>
</modification>
Tags
You can set multiple file locations by spiting up the file locations using commas. The modification system uses the PHP function glob.
Direct file path.
<file path="admin/controller/common/column_left.php">
Using braces allows for selecting multiple files and not having to repeat the code operation multiple times.
<file path="system/engine/action.php|system/engine/loader.php|system/library/config.php|system/library/language.php">
also allows braces
<file path="system/{engine,library}/{action,loader,config,language}*.php">
Please note that all file paths must start with either admin, catalog or system. You can also use a wildcard to search for multiple directories and files.
Search
Search code
Allowed Attributes
- trim=”(true|false)”
- regex=”(true|false)”
- index=”(number)”
Example:
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Modification Default</name>
<version>1.0</version>
<author>OpenCart Ltd</author>
<link>http://www.opencart.com</link>
<code>webocreation_mod_1</code>
<file path="catalog/controller/common/home.php">
<operation>
<search trim="true|false" index="1"><![CDATA[
$data['column_left'] = $this->load->controller('common/column_left');
]]></search>
<add position="replace" offset="1"><![CDATA[
test123
]]></add>
</operation>
</file>
</modification>
Add
The code to replace the search with.
Allowed Attributes
- trim=”(true|false)”
- position=”(replace|before|after)”
- offset=”(number)”
(note position can not be used if regex is set to true in the search attribute).
Example
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Modification Default</name>
<version>1.0</version>
<author>OpenCart Ltd</author>
<code>webocreation_modification_default</code>
<link>http://www.opencart.com</link>
<file path="catalog/controller/common/home.php">
<operation>
<search trim="true|false"><![CDATA[
$data['column_left'] = $this->load->controller('common/column_left');
]]></search>
<add position="replace|before|after" trim="true|false" offset="2"><![CDATA[
test123
]]></add>
</operation>
</file>
</modification>
Regex
Allowed Attributes
- limit=”(number)”
Example:
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Regex Example</name>
<version>1.0</version>
<author>OpenCart Ltd</author>
<code>webocreation_regexexample</code>
<link>http://www.opencart.com</link>
<file path="system/{engine,library}/{action,loader,config,language}*.php">
<operation>
<search regex="true" limit="1"><![CDATA[
~(require|include)(_once)?\(([^)]+)~
]]></search>
<add><![CDATA[
$1$2(modification($3)
]]></add>
</operation>
</file>
</modification>
If you use a regex you can not use attributes position, trim or offset as these are handled by the regular expression code you write. The limit attribute is still available.
If, for example, you want to change the 3rd ‘foo’ to ‘bar’ on the following line:
lorem ifoopsum foo lor foor ipsum foo dolor foo
^1 ^2 ^3 ^4 ^5
Run:
s/\(.\{-}\zsfoo\)\{3}/bar/
Result:
lorem ifoopsum foo lor barr ipsum foo dolor foo
^1 ^2 ^3=bar ^4 ^5
Let us know if you have any more ideas.
Can you show an example how to use install.php to run some queries on the database after installation of a modification? I need to insert a row, get last insert id and insert rows in other tables. It doesn’t seem to work in opencart 3.
Hi Constantin,
Opencart 3 OCMOD has changed a little bit so need to use the install method and uninstall method in the controller.
Hi, how can i create a table through extension?? I’ve created the client side things clearly through xml but stuck at creating a table. In the controller i’ve used only install() and uninstall() methods and didn’t use index() or validate() method.The install() and uninstall() points to the model that I’ve created along which has the methods with sql code for creating and dropping the tables. Sorry if i was wrong , point out any mistakes or explain me how to Mr Rupak.
Hi Gowtham,
You are in the right direction.
1. Make sure you create a new table
2. Make sure you make all files in the extension folder.
3. Make sure in install method be something like below:
public function install()
{
$this->load->model(‘extension/module/YOURMODULE’);
$this->model_extension_module_YOURMODULE->createYOURMODULETable();
}
4. In model it is something like:
public function createYOURMODULETable()
{
$this->db->query(“CREATE TABLE `” . DB_PREFIX . “……….”);
}
Hope it helps
nice articel very helfull, i would to remove transaction with ocmod please help me
can u explain how offset works ?
is there an if arguments ?
if search not exist then use another search
Thanks for Opencart documentation