Post by Jsf » Thu May 25, 2023 6:01 am

Hi,
I'm trying to move from Ocmod to Events and don't know what is the best way to modify admin model files, for example just change one line of code.
I want to make a simple mod to improve getProducts function in admin/model/catalog/product.php file (add % at the beginning)
replace:

Code: Select all

$sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
to:

Code: Select all

 $sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
Please give me an example or hint how I can do this with events ?
Last edited by Jsf on Thu May 25, 2023 3:47 pm, edited 1 time in total.

User avatar
Jsf
New member

Posts

Joined
Sun Jan 26, 2014 4:08 am

Post by straightlight » Thu May 25, 2023 6:18 am

Jsf wrote:
Thu May 25, 2023 6:01 am
Hi,
I'm trying to move from Ocmod to Events and don't know what is the best way to modify admin model files, for example just change one line of code.
I want to make a simple mod to improve getProducts function in admin/model/catalog/product.php file (add % at the beginning)
replace:

Code: Select all

$sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
to:

Code: Select all

 $sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
Please give me an example or hint how I can do this with events ?
OC version. Your question is about OCMod, not about Events. Events do not allow to override lines with PHP files, only from PHP to TWIG files.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Jsf » Thu May 25, 2023 3:45 pm

I know well how OCMOD works and how to make such a modification with OCMOD. But it is possible to change model files with events? What is the best practice if I want to modify some SQL queries? How to do that with events only?

User avatar
Jsf
New member

Posts

Joined
Sun Jan 26, 2014 4:08 am

Post by Johnathan » Thu May 25, 2023 9:22 pm

You can't do that with Events, unfortunately. Instead, you have to add an "after" event that hooks into that function, and then re-run the database query with your own SQL to change the output. At least, that's the only way I know of.

If this is for your own usage I would recommend using vQmod instead. It's a lot quicker and easier for small changes like this. You can find it here:

First Install This: https://github.com/vqmod/vqmod/releases
Then Install This: https://github.com/vqmod/opencart/releases

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by JNeuhoff » Thu May 25, 2023 10:40 pm

You could write up an event handler for the trigger controller/catalog/product/getProducts/before so there won't be duplicate DB calls.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Jsf » Fri May 26, 2023 4:13 am

Johnathan wrote:
Thu May 25, 2023 9:22 pm
You can't do that with Events, unfortunately. Instead, you have to add an "after" event that hooks into that function, and then re-run the database query with your own SQL to change the output. At least, that's the only way I know of.

If this is for your own usage I would recommend using vQmod instead. It's a lot quicker and easier for small changes like this. You can find it here:

First Install This: https://github.com/vqmod/vqmod/releases
Then Install This: https://github.com/vqmod/opencart/releases
Thanks for answer. vQmod seems like a good choice. I have many modifications that touch just a few lines of SQL queries. For example, it improves the search, takes more columns from the database, etc. It really doesn't seem like a logical solution to run the SQL query again if you're actually doing the same thing.

Events seem like a good solution to send alert emails or delete something from another database after some event, but it's hard for me to imagine how hundreds of modules from different developers would work in OC 4 version.

User avatar
Jsf
New member

Posts

Joined
Sun Jan 26, 2014 4:08 am

Post by Jsf » Fri May 26, 2023 4:20 am

JNeuhoff wrote:
Thu May 25, 2023 10:40 pm
You could write up an event handler for the trigger controller/catalog/product/getProducts/before so there won't be duplicate DB calls.
Could you write a more detailed example? I think it would be useful for most people, because there is so little information about the events. And it would be very good according to the example of my first post when you just need to change one line of code in the admin model file?

User avatar
Jsf
New member

Posts

Joined
Sun Jan 26, 2014 4:08 am

Post by straightlight » Fri May 26, 2023 7:10 pm

Jsf wrote:
Fri May 26, 2023 4:20 am
JNeuhoff wrote:
Thu May 25, 2023 10:40 pm
You could write up an event handler for the trigger controller/catalog/product/getProducts/before so there won't be duplicate DB calls.
Could you write a more detailed example? I think it would be useful for most people, because there is so little information about the events. And it would be very good according to the example of my first post when you just need to change one line of code in the admin model file?
viewtopic.php?t=227710

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by khnaz35 » Mon May 29, 2023 7:58 pm

You can do something like this.

First, let's register the event. This will typically be done in your admin/controller/extension/module/yourmodule.php file (replace 'yourmodule' with the name of your module).

Code: Select all

$this->load->model('setting/event');
$this->model_setting_event->addEvent('yourmodule', 'admin/model/catalog/product/getProducts/after', 'extension/module/yourmodule/getProductsAfter');
Then, you would create the method that gets called when the event fires, in this case it's 'getProductsAfter'. This would be in your admin/controller/extension/module/yourmodule.php file.

Code: Select all

public function getProductsAfter(&$route, &$args, &$output) {
    if (isset($args[0]['filter_model'])) {
        $model = $args[0]['filter_model'];
        //Modify the SQL query
        $output = str_replace("p.model LIKE '" . $this->db->escape($model) . "%'", "p.model LIKE '%" . $this->db->escape($model) . "%'", $output);
    }
}
Remember: Always back up your files before making modifications and thoroughly test all changes before applying them to a live site.

Urgent Questions shoot here: khnaz35@gmail.com
Enjoy nature ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia

Post by Jsf » Wed May 31, 2023 7:49 pm

Thank you! Good example. Can you also give an example how I can modify language file? I need to add new language variable.

User avatar
Jsf
New member

Posts

Joined
Sun Jan 26, 2014 4:08 am

Post by khnaz35 » Wed May 31, 2023 10:04 pm

If you want to use an event system to modify the language file, it's important to note that OpenCart's events system is designed to trigger functionality based on actions in the system. It isn't designed to modify files directly, including language files. (Perhaps i am wrong , maybe some pro can verify it)

This is a bit of a workaround

First, register the event. This will be done in your admin/controller/extension/module/yourmodule.php file (replace yourmodule with the name of your module).

Code: Select all

$this->load->model('setting/event');
$this->model_setting_event->addEvent('yourmodule', 'admin/language/*/before', 'extension/module/yourmodule/modifyLanguage');
Then, you would create the method that gets called when the event fires, in this case it's modifyLanguage. This would be in your admin/controller/extension/module/yourmodule.php file.

Code: Select all

public function modifyLanguage(&$route, &$args, &$output) {
    $file = DIR_LANGUAGE . $args[0] . '/' . end(explode('/', $route)) . '.php';
    $data = [];
    include($file); // This will populate $data with the current language variables

    if (!isset($data['new_variable'])) {
        $content = file_get_contents($file);
        $content .= "\n$_['new_variable'] = 'Your new variable value';";
        file_put_contents($file, $content);
    }
}
Ps. remember the drill of backup.

Urgent Questions shoot here: khnaz35@gmail.com
Enjoy nature ;) :) :-*


User avatar
Active Member

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia
Who is online

Users browsing this forum: No registered users and 180 guests