Home Opencart Opencart 3 OCMOD coding tutorial

Opencart 3 OCMOD coding tutorial

In this tutorial, we are showing you how to write code in OCMOD and modify the core files of OpenCart. Here we will show the flows that happened when someone uploads the ***.ocmod.zip file, then create an install.xml file to show the module link in the left column and give you details of Ocmod documentation in Opencart 3. For Opencart 4 OCMOD tutorial click here

As per the core code when you upload the ***.ocmod.zip folder from Extensions >> Installer, the following things will happen:

  1. Check if the user has permission. You will get an error like:
    “Warning: You do not have permission to modify modifications!”
  2. Check if there is an install temporary folder zip already there.
  3. If it found the file and the file created time is less than 5 seconds then it tries to unlink or remove the file, if it cannot remove the file then it throws an error saying:
    “Extension installation taking place please wait a few seconds before trying to install!”
  4. It checks whether the uploaded zip file ends with .ocmod.zip
  5. It checks if there are any other file uploading errors, UPLOAD_ERR_OK: Value: 0; There is no error, and the file uploaded with success. If there is any error it will show errors like:
    “File could not be uploaded!”
  6. If everything is good then it creates a .tmp file and performs function move_uploaded_file.
  7. Then it adds the logs in the database table oc_extension_install with the name ***.ocmod.zip and the extension download id. “oc_” is the database prefix.
  8. Then it starts the installation, from the controller “ControllerMarketplaceInstall”, it makes sure the file name is stored in the session. If the file is not in that session then it shows an error like this:
    “File could not be found!”
  9. Then it starts unzipping the ***.ocmod.zip and if it cannot unzip the ocmod.zip file then it shows an error like:
    “Zip file could not be opened!”
  10. After unzipping it checks whether the unzip folder contains “upload/” or not, likewise, it checks for the allowed directories to be written or not. Opencart allows the following directories only:
    • ‘admin/controller/extension/’,
  11. So while creating a custom opencart module better to work on these folders only. If you put it in another folder then it may give you the following error:
    “The directory %s is not allowed to be written to!”
  12. It checks for the directory structure of the zip and whether it matches with the Opencart directory structure.
  13. Then it will insert in the database table oc_extension_path with extension install id.
  14. Then it will start installing the /install.xml, if an XML file exists then it is put into the database. It checks for the “code” name in the XML and checks if the values already exist in the database. If everything is good then it inserts into the database table oc_modification with extension_install_id, name, code, author, version, link, XML, status, and date_added.
  15. Then it unlinks or removes the temporary folder.
  16. Finally, it gives the success message.

With the flow steps above the folder structure for the ocmod.zip is install.xml and upload. Inside the upload/ folder put the OpenCart folder and files.

In the install.xml it should be something like where the ******* is your custom thing.

ocmod code starter
<?xml version="1.0" encoding="utf-8"?>
    <file path="*******">
            <search><![CDATA[ ******* ]]></search>
            <add position="*******"><![CDATA[*******]]></add>

Don’t forget <code>, although it is not mentioned in the Modification system documentation https://github.com/opencart/opencart/wiki/Modification-System

You can use multiple file operations. There are two operations search and add. With the Add operation, you can replace, add before, or add after.

<file path="">
        <add position=""><![CDATA[]]></add>

Now check out the previous blog posts where we have listed all the documentation of OCMOD.

While zipping the folder zip the install.xml and upload/ folder, not the main folder. install.xml is not mandatory.

If you are trying to create a table or insert data in the table then you need to create the install method in your controller. Like:

module install database

In the install method, you can implement logic as needed for your module when it is installed.

//Check it in admin/controller/extension/extension/module.php install method.
// Call install method if it exists
$this->load->controller('extension/module/' . $this->request->get['extension'] . '/install');

Similarly, you can implement the logic for the uninstalling method when it is uninstalled.

We created a free module with OCMOD to show the Module link in the left menu with the following XML:

<?xml version="1.0" encoding="utf-8"?>
    <name>Show Module Link At Left Menu</name>
    <author>Rupak Nepali</author>
    <description>Show module link at left menu</description>
    <file path="admin/controller/common/column_left.php">
            <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()

Steps for making changes to the core files of OpenCart

  1. In your local make the changes in the core of the files and keep track of the files
  2. Then create the install.xml and add those changes in install.xml
  3. Then remove the changes of the core files that you make the changes.
  4. Then zip it with ***.ocmod.zip and upload in Extensions >> Installer
  5. Then clear the cache in Extensions >> Modifications
  6. You are set.

Testing Tips for the Developer

Testing is hard for OCMOD changes if you have to change in XML, zip, upload from the installer, and clear in modifications. So the tip for developers is they can place the install.xml in the system/ folder (not sure if it is supported in Opencart version, then make changes in the install.xml and you don’t need to zip and upload, you can clear in modifications and you can check the changes. If you don’t want to clear the cache in modifications then you can go through this blog Opencart cache clear.

If their search code is not found then it shows an error like the below in the Ocmod log:

FILE: admin/controller/common/column_left.php
CODE: if ($this->user('access', 'marketplace/modification')) {

The Search Code provides the following attributes:

  • trim=”(true|false)”
  • regex=”(true|false)”
  • index=”(number)”

The add code provides the following attributes:

  • trim=”(true|false)”
  • position=”(replace|before|after)”
  • offset=”(number)”

Other you can read at https://webocreation.com/ocmod-documentation/

Let us know if you have any suggestions or questions.



Please enter your comment!
Please enter your name here