Read More
Read More
Menu Close

    Creating own library in opencart

    Why load model again and again in opencart, just create your own library and register it with opencart and use it as you need and when you need.

    If we see the default flow for opencart, if you want to fetch any data from database then you have to create model that provides data set and to use model, we have to load it on desired controller.

    If we create library and register it with opencart then we can use it as we use other libraries(customer, cart). There is also added advantage of using library that we can not load model both the sides (admin and catalog), if we create it one side only, so this problem also be resoved.

    Now let’s see how to do this-

    At the very first, create your class file in opencart directory at system->library and write down your methods that you want to use as below.

    Searching for an experienced
    Opencart Company ?
    Read More


    So the class file is ready, now it must be registered. To register it we have put some code in our initial files like startup.php, front.php etc but I’ll use front.php because on index.php file, this file is loaded at the last. So alll the required methods or files will be loaded till, this library will get registered. We can do it by ocmod and vqmod.


    We need to put above two lines in constructor of front.php and your library is ready to use any where in catalog. To use it in admin side, same thing need to be done at admin side. Now methods of above class could be used as below.


    There also an another way to call library methods without putting code in front.php file. Use below code to call method where ever you want to access these.


    Now this class is limited to controller in which it is used as above.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • Pavlo Dovzhenko
    Thank you very mutch for this tutorial.

    I think i have to share my experience with the library creation with this tutorial.
    I’m sorry for my not perfect english language knowledge.
    I’ve working with this tutorial whith the goal to create alternative library for the Pagination (Opencart 2.02).

    Unfortunatedly this tutorial doesn’t helped me because the library doesn’t worked with this advices.
    Probably i had put the registry->set in the wrong place of the front.php, but i have tryed all logical variants.
    Placing of the registration code in the controller hasn’t given any positive result.

    The library starts working without any additional changes in the system files like as front.php, index.php etc.
    I just put the file with my library to the system/library, and my new class has been created in the controller and works properly.

    • Webkul Support
      Hello, Greetings for the day, hope you are doing great.
      Thank you for your appreciation, it’s been a long time that blog has been posted. Since then, opencart has been changed so much, but still you have to create an object of your class. Opencart system will load the file but it won’t create the object. Opencart provides function by which you can easily use your own library without any issue.
      As you said that your library file is working fine after simply creating a file and class.If it is working in the same way, please send me the file and let me know the opencart version. Have a great day ahead! Thanks Avneesh Kumar
  • Rajitha Bandara
    This tutorial is really helpful. For OC 2.3, how should I load library in template files instead of controller? I’m building a custom theme and think I should not touch controller in this case.
  • Back to Top