Custom libraries in Opencart (v2)

Custom libraries in Opencart (v2)

Creating custom libraries in Opencart can be very helpful. Creating additional functionality with a library is easy. In this article is shown how to create a custom library and how to use it, including autoloading.

Creating custom libraries in Opencart can be very helpful. Creating additional functionality with a library is easy. In this article is shown how to create a custom library and how to use it, including autoloading, if that is preferred.

Some assumptions before we start

I assume that you know what a library is (in fact a class with some methods, we call a library) and that it has to be placed in the folder 'system/library' of your Opencart installation.

The name for our library in this example will be 'Megatron' (just to make it obvious). So we will have a class defined in a file named 'megatron.php' in folder 'system/library'. The name of the class is 'Megatron'.

How conventionally a library is loaded

Before you can use a library, you have to load it first. We do this usually in a controller with:

$this->load->library('megatron');

This looks very spectacular, but is not much more than that Opencart will look for a 'php' named 'megatron' in the folder 'system/library'.

From there on it would be working like this in daily practice:

$this->load->library('megatron');
// megatron is a singleton !!!
$megatron = Megatron::get_instance($this->registry);

$firing_mode_current = $megatron->getCurrentFiringMode();
$firing_modes_available = $megatron->getAllFiringModes();

return [
    'current' => $firing_mode_current,
    'available' => $firing_modes_available
];

As you can see is the library first loaded, then with get_instance the object (singleton) is assigned to variable $megatron.

Let's assume that our Megatron is required all over the place (our Opencart that is), then it would be usefull to autoload (not autobod) the library (instantiate) when starting Opencart.

Autoloading a custom library

Autoloading requires a bit more work, but is not that difficult either to achieve.

After we created our library, we need to register it. In our situation we will do this with a modification, that we will install with menu option:

Extension -> Extension installer

The customization file looks like this:

megatron.ocmod.xml
<?xml version="1.0" encoding="utf-8"?>
<modification>
    <code>RegisterMegatronLib</code>
    <name>Register Megatron Library</name>
    <version>1.0</version>
    <author>Megatron</author>
    <link>http://www.megatron.author</link>
    <file path="catalog/controller/startup/startup.php">    
        <operation>
            <search>
                <![CDATA[
$this->registry->set('openbay', new Openbay($this->registry));
                ]]>
            </search>
            <add position="after">
                <![CDATA[
$this->registry->set('megatron', new Megatron($this->registry));
                ]]>
            </add>
        </operation>    
    </file>  
</modification>

What is modified

  • File to be modified: catalog/controller/startup/startup.php.

  • What position is modified: (just after)

    $this->registry->set('openbay', new Openbay($this->registry));

  • What is inserted:

    $this->registry->set('dbimg', new OurCustomLib($this->registry));

How to use an autoloaded library

Let's assume this OurCustomLib has a method named getTheHotJuice, then it works as following:

$firing_mode_current = $this->megatron->getCurrentFiringMode();
$firing_modes_available = $this->megatron->getAllFiringModes();

return [
    'current' => $firing_mode_current,
    'available' => $firing_modes_available
];

Well, there it is, the megatron library is now registered and therefor like any other autoloaded library within Opencart accessible through:

$this->megatron

More from same category