Libraries can be very useful to add extra functionality to Opencart.
In our implementation we have the 'dbimg' library, which contains a load of functions which are useful to our implementation.
$this->load->library('dbimg'); // dbimg is a singleton !!! $dbimg = Dbimg::get_instance($this->registry); $product_id = $dbimg->getProductId(); return $product_id;
Autoloading a library is a bit more work, but not that difficult either.
After we created our library, we have to register it. We do this with an modification.
We assume that our library is named 'ourcustomlib.php' as in class 'OurCustomLib'.
The customization looks like this:
<?xml version="1.0" encoding="utf-8"?> <modification> <code>RegisterOurCustomLib</code> <name>Register OurCustomLib Lib</name> <version>1.0</version> <author>Lord Byron</author> <link>http://www.lord-byron.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('dbimg', new OurCustomLib($this->registry)); ]]> </add> </operation> </file> </modification>
$this->registry->set('openbay', new Openbay($this->registry));
$this->registry->set('dbimg', new OurCustomLib($this->registry));
Let's assume this
OurCustomLib has a method named
getTheHotJuice, then it works as following:
$juice = $this->ourcustomlib->getTheHotJuice();
That's all there is.