Replace functions with OCMOD in Opencart

Replace functions with OCMOD in Opencart

In this article I will show how you can override a system helper function in Opencart without modifying the core (which you should never do).

Opencart is great software for running a webshop, it even supports multiple shops with a single installation. For a customer I am using version 2 of Opencart which is using OCMOD modification management.

With OCMOD the modifications are described in an XML file that is installed as an extension in Opencart. The modifications describe code that overrides the core contents in an Opencart installation. But the core itself is never changed.

Caching modifications

When using this way of modifying Opencart the modifications are not edited in the core of Opencart, but cached in the folder /system/storage/modification. Don't modify the contents of this folder by hand, but use the Extension installer instead to communicate changes.

{.

install.ocmod.xml

I created a customization XML for modifications that are somewhat unique to a certain implementation of Opencart. For version 2.x of Opencart I created an install.ocmod.xml with our own identity.

The name of the file should be something.ocmod.xml and not something.xml. It will run into an error otherwise.

Skeleton of this file

<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Enovision Customizations</name>
    <code>EnovisionCustom</code>
    <version>99.9</version>
    <author>J.J. van de Merwe</author>
    <link>http://www.enovision.nl</link>
    <file path=".....">
        <operation>
            <search>
                <![CDATA[
start here for starting at first position (for search don't add the blanks or tabs)
                ]]>
            </search>
            <add position="before">
                <![CDATA[
start here for starting at first position
                ]]>
            </add>
        </operation>
    </file>
</modification>

Realtime sample, adding composer support

This is a realtime modification that can be used to add composer support to Opencart to add external libraries.

Create a file named composer.ocmod.xml and add the following content to it:

<?xml version="1.0" encoding="utf-8"?>
<modification>
    <code>AddComposerSupport</code>
    <name>Add Composer Support</name>
    <version>1.0</version>
    <author>J.J. van de Merwe</author>
    <link>http://www.enovision.nl</link>
    <file path="system/startup.php">
        <operation>
            <search position="after">
                <![CDATA[
spl_autoload_extensions('.php');
                ]]>
            </search>
            <add position="insert">
                <![CDATA[
require DIR_SYSTEM . '../vendor/autoload.php';
                ]]>
            </add>
        </operation>
    </file>  
</modification>

After this modification is installed and the cache is updated, you can check the results on the server in folder: /system/storage/modification/startup.php.

It should now contain:

spl_autoload_register('library');

require DIR_SYSTEM . '../vendor/autoload.php';

Where to put the composer.json?

To make this realtime sample complete, we are going to add mustache support to Opencart.

Open a terminal on folder: /system of Opencart. Now enter:

composer require mustache/mustache

Mustache will be installed in a newly created vendor folder within the system folder. It will also create a composer.json file.

How to use mustache within Opencart I will explain in another article.

More from same category