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 module link in the left column and give you details of Ocmod documentation in Opencart 3.

As per the core code when you upload the ***.ocmod.zip folder from Extensions >> Installer, 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 is any other file uploading errors, UPLOAD_ERR_OK: Value: 0; There is no error, the file uploaded with success. If there is any error it will show error like:
    “File could not be uploaded!”
  6. If everything is good then it creates a .tmp files and performs function move_uploaded_file.
  7. Then it adds the logs in database table oc_extension_install with the name ***.ocmod.zip and the extension download id. “oc_” is 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 error like:
    “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 error like:
    “Zip file could not be opened!”
  10. After unzipping it checks whether the unzip folder contains “upload/” or not, likewise, it checked for the allowed directories to be written or not. Opencart allows following directories only:
    • ‘admin/controller/extension/’,
      ‘admin/language/’,
      ‘admin/model/extension/’,
      ‘admin/view/image/’,
      ‘admin/view/javascript/’,
      ‘admin/view/stylesheet/’,
      ‘admin/view/template/extension/’,
      ‘catalog/controller/extension/’,
      ‘catalog/language/’,
      ‘catalog/model/extension/’,
      ‘catalog/view/javascript/’,
      ‘catalog/view/theme/’,
      ‘system/config/’,
      ‘system/library/’,
      ‘image/catalog/’
  11. So while creating custom opencart module better to work on these folders only. If you put on other folder then it may give you following error:
    “The directory %s is not allowed to be written to!”
  12. It checks for the directories structure of the zip whether it matches with Opencart directories 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 XML file exists then it 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 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 taking above the folder structure for the ocmod.zip are 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"?>
<modification>
    <name>*******</name>
    <version>*******</version>
    <author>*******</author>
    <link>*******</link>
    <code>*******</code>
    <description>*******</description>
    <file path="*******">
        <operation>
            <search><![CDATA[ ******* ]]></search>
            <add position="*******"><![CDATA[*******]]></add>
        </operation>
    </file>
</modification>

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 add operation you can do replace, add before or add after.

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

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 Module link in the left menu with the following 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>

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 tracks 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 developers

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

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

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

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/blog/ocmod-documentation/

Let us know if you have any suggestions or questions.

LEAVE A REPLY

Please enter your comment!
Please enter your name here