Hi me Rupak Nepali again, today I am going to cover about OpenCart OCMOD documentation and tutorial for the developer and show how to start coding in OCMOD to change the files and extend functionalities.

OCMOD is a system that allows store owners to be able to modify their store by uploading a compressed file that contains XML, SQL, and PHP files. If an OCMOD is developed correctly it can modify how a users OpenCart store without changing any of the core files, This means that if a modification is removed none of the original OpenCart files need to be restored or fixed.
OCMOD is based upon Qphoria’s VqMOD system.

Opencart 3 install uninstall method in OCMOD

The detail documentation of OCMOD for the developer is at  https://github.com/opencart/opencart/wiki/Modification-System  

Both have 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.

Main feature differences for the programmers are:

Both OCMOD and VqMOD have:
Replace, Before, After, Regex, Offset, Limit, ignoreif, error
Missing in OCMOD which had in VqMOD are:
Attributes top, bottome, ibefore, iafter

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 to directory of your OpenCart installation.

install.sql

  • Add any create, drop, insert and update SQL quires here. Make sure each query ends with ;

install.php

  • If the modification requires any custom PHP code.

install.xml

Demo to install the OCMOD

Let 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 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 wildcard * to search 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>
    <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>
    <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>
    <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 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’s us know if you have any more ideas.

[scribd id=395849215 key=key-aHXUZoJCekoOdRd7QKuu mode=scroll]

2 COMMENTS

  1. 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.

      install uninstall database in OCMOD opencart

LEAVE A REPLY

Please enter your comment!
Please enter your name here