Reading list Switch to dark mode

    Creating own library in opencart

    Updated 9 March 2016

    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 ?
    Find out More

    03

    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.

    01

    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.

    04

    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.

    01

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

    . . .

    Leave a Comment

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


    3 comments

  • Pavlo Dovzhenko
    • Webkul Support
  • Rajitha Bandara
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home