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.
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.
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.