I'm working on an OpenCart project, that requires a lot of customization. for my project I have to change something in the cart library (system/library/cart.php).
I would have to call a custom function that's defined inside the product model (catalog/model/catalog/product.php).
In a controller, loading a Model and using its functions is easy:
$this->load->model("catalog/product");
$this->model_catalog_product->customFunction();
But how do you load a model outside a controller? You can't create a new instance of the model, I already tried that:
require_once("catalog/model/catalog/product.php");
$a_model = new ModelCatalogProduct();
This obviously doesn't work cause models weren't intended to be used in such a way.
I also tried to use the scope resolution operator ( ModelCatalogProduct::customFunction()) It doesn't work either.
I could pass all the required info as arguments, but I would rather use the model inside the cart library class, cause the changes would be global.
Is it even possible to load a model outside a controller in OpenCart?
If it's only one method that you need to copy, you would be best adding a method to the Cart
class itself. The Cart
class will work with the $this->db->query()
calls as it already has $db
assigned to it even though it's not a Controller/Model
Should you wish to do this, you could do something similar to the following
public function test() {
global $loader, $registry;
$loader->model('catalog/product');
$model = $registry->get('model_catalog_product');
$result = $model->getProduct(123);
}
You are able to load a model outside a controlled.
If You need to load a model inside of another model, You could load it the very same way using $this->load->model('my_module/my_model');
.
If You need to load a model inside of template file or other custom PHP script, look at the index.php
file where the Registry
is instanciated - You would need to instanciate it the same way. So Your custom code could look like:
$registry = new Registry();
$my_model = $registry->load->model('my_module/my_model');
$my_model->customFunction();
Anyway I highly recommend not to edit/change the core library files unless You are sure there is no other way how to implement/do what You need.
As Jay Gilford proposed I would implement that function or it's call to the catalog/checkout/cart.php
, potentially confirm
or success
depending on the scope and functionality You would like to implement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With