I am adding this code via modification into system cart library to access my custom model located in catalog/model/extension/folder_name/file_name:
public function __construct($registry) {
global $loader;
$loader->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
But it says: Fatal error: Uncaught Error: Call to a member function model() on null in opencart 2.3.0.2
While this code is fine when I work on opencart 2.2.0.0.
Please help...
you got the idea right, just a syntax error.
In OpenCart in a controller file the loader is accessed via $this->load->model()
But in system/library/cart/cart.php your code should look like this
public function __construct($registry) {
$registry->get('load')->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
This is because you are accessing directly in system/library/cart/cart.php, where you have access to the
$registry
from the construct but no__get()
and__set()
functions like the controllers and models have.The cool thing is, the $this->load->model method actually checks where the cart is being called (is it from
catalog
folder oradmin
folder) and loads accordingly.You can be safe to load a model in
cart.php
because it is only called incatalog
folder, but be careful adding such code to system/library/request.php which is loaded both incatalog
andadmin
. this will creat errors.