Post by straightlight » Sun Jan 31, 2021 9:33 am

catalog/controller/extension/advertise/google.php provides all the /after events where multiple examples shows how to call a TWIG file from the controller as well.

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 kestas » Sun Jan 31, 2021 9:20 pm

JNeuhoff wrote:
Sat Jan 30, 2021 8:16 pm
kestas wrote:
Sat Jan 30, 2021 5:40 pm

All sounds good, but if I correctly understand there no possibility, using events, add to the twig files particular code... For example if I need additional column in order list (admin) to display which shipping method was used for the order... If it is possible to add specific code to twig files using events, can anyone give an example of how to do this properly?
Take a look at this forum post which should answer your question.
Thank you @JNeuhoff. Got it.

Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am

Post by OSWorX » Sun Jan 31, 2021 9:51 pm

straightlight wrote:
Sun Jan 31, 2021 9:33 am
catalog/controller/extension/advertise/google.php provides all the /after events where multiple examples shows how to call a TWIG file from the controller as well.
Would be fine .. but is not true.
Don't know why your are posting such nonsens - maybe it's sunday and you have nothing to do?

That file is adding only a few data to the $data array, and then adding only simple javascript code to the template by replacing the body tag.
This has nothing to do with replacements inside a twig template when someone wants to add some new code fragments, remove or change.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by xxvirusxx » Sun Jan 31, 2021 11:23 pm

OSWorX wrote:
Sat Jan 30, 2021 7:02 pm
A simple

Code: Select all

$tpl = str_replace( $search, $replace, $tpl );
will do that (of course $search & $replace defined before).
If a file has more identical lines how you can search and replace only 1 line?
In ocmod is search index=x

My Converted Modules | Opencart Upgrade Service | Free Dashboard Maps | Buy me a beer | My Custom OC 3.0.3.8


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by straightlight » Sun Jan 31, 2021 11:26 pm

OSWorX wrote:
Sun Jan 31, 2021 9:51 pm
straightlight wrote:
Sun Jan 31, 2021 9:33 am
catalog/controller/extension/advertise/google.php provides all the /after events where multiple examples shows how to call a TWIG file from the controller as well.
Would be fine .. but is not true.
Don't know why your are posting such nonsens - maybe it's sunday and you have nothing to do?

That file is adding only a few data to the $data array, and then adding only simple javascript code to the template by replacing the body tag.
This has nothing to do with replacements inside a twig template when someone wants to add some new code fragments, remove or change.
$snippet = $this->load->view('extension/advertise/google_dynamic_remarketing_searchresults', $data);

// Insert the snippet after the output
$output = str_replace('</body>', $snippet . '</body>', $output);
And, no, it's not because it's sunday but because it's Sunday. People can still add new codes regardless if they want to replace or add new variables. The total and free e.gs are showing in there.

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 OSWorX » Mon Feb 01, 2021 1:13 am

xxvirusxx wrote:
Sun Jan 31, 2021 11:23 pm
OSWorX wrote:
Sat Jan 30, 2021 7:02 pm
A simple

Code: Select all

$tpl = str_replace( $search, $replace, $tpl );
will do that (of course $search & $replace defined before).
If a file has more identical lines how you can search and replace only 1 line?
In ocmod is search index=x
Seems nobody here is reading!
OpenCart 4.x DOES NOT HAVE ANY OCMod further!
And we are talking here about the (new) possibilities you will have with the (new) Events system - while still possible also now in 3.x

Conclusion of this: whatever was possible until now with OCMod like file, operation, search, add IS NOT anymore.
The only left from former ocmods is the install.xml with the header lines only (code, name, author etc.) and the upload folder.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by JNeuhoff » Mon Feb 01, 2021 1:51 am

OpenCart 4.x DOES NOT HAVE ANY OCMod further!
And we are talking here about the (new) possibilities you will have with the (new) Events system - while still possible also now in 3.x
Here we have a chance for a clean reset with regards to an improved quality of future OC extensions, no more dirty OCMod XML scripts for modifying code or PHP templates! Though I am afraid people will find new ways of writing poor quality extensions, especially for web themes.

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by xxvirusxx » Mon Feb 01, 2021 3:43 am

OSWorX wrote:
Mon Feb 01, 2021 1:13 am
Seems nobody here is reading!
I have read. That's why I asked.
For example in the model there are 3 lines identical.

line 1. <filter_id=2>
line 45. <filter_id=2>
line 67. <filter_id=2>

And using event I want to add some code after line 45.
How event know, in particular this command to add my code after line 45?

Code: Select all

$tpl = str_replace( $search, $replace, $tpl );
in this case $search will not search all <filter_id=2> ? then my code will by added after/before all lines?

My Converted Modules | Opencart Upgrade Service | Free Dashboard Maps | Buy me a beer | My Custom OC 3.0.3.8


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by OSWorX » Mon Feb 01, 2021 5:09 pm

xxvirusxx wrote:
Mon Feb 01, 2021 3:43 am
For example in the model there are 3 lines identical.

line 1. <filter_id=2>
line 45. <filter_id=2>
line 67. <filter_id=2>

And using event I want to add some code after line 45.
How event know, in particular this command to add my code after line 45?

Code: Select all

$tpl = str_replace( $search, $replace, $tpl );
in this case $search will not search all <filter_id=2> ? then my code will by added after/before all lines?
Okay, what about we are talking here?
You are mentioning a "model" ..

One is the controller in which the event function is stored - and called later see below).
The event itself can be triggered by several points (= events):

1. controller
2. model
3. template

1. & 2. have only the route (= string) and the data (= array) as arguments.
3. has also the parsed template - called here $output or whatever else (= string).

Cases 1. & 2 can be called by before and/or after, means before = raw, after = manipulated.
Case 3 only with after (because with before we have no template).

Which means - and that about this discussion here started - whenever you want to "manipulate" the raw template code, you have to get it currently with a custom function.
As JNeuhoff decribed and what he wanted to have in the core code with his pull request (see GitHub).

When you fetched the template code, you can edit any code and any line(s).

Just to mention finally: when the event is called, he could also return a complete new template, not only manipulating the $route, $data and $output by reference.
And any controller and model can be called inside this events - exisiting and new (custom).

p.s.: should I am wrong in one point, please post it .. thx.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by JNeuhoff » Mon Feb 01, 2021 5:25 pm

And using event I want to add some code after line 45.
How event know, in particular this command to add my code after line 45?
Seriously, that only takes a few seconds to find an answer via a Google search.

E.g. using the search term 'php string replace nth occurrence' comes up with these hits:

https://stackoverflow.com/questions/199 ... n-a-string
https://gist.github.com/VijayaSankarN/0 ... 7d276b72bd

Override Engine * Integrated VQMod * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Rich Snippets * Google Tag Manager * Export/Import Tool * SpamBot Buster * Survey Plus


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by xxvirusxx » Mon Feb 01, 2021 5:45 pm

@OSWorX
Tks for explanations

@JNeuhoff
I didn't think last night :laugh:

My Converted Modules | Opencart Upgrade Service | Free Dashboard Maps | Buy me a beer | My Custom OC 3.0.3.8


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by cbaldoni » Thu Mar 03, 2022 11:53 pm

Dear all, this is a very interesting topic, as I'm trying exactly to reach the same goal by events. What I need to achieve is to display some detailed instruction to customer at order checkout successfull completion, depending on the composition of customer's order. I do not won't to modify via OCMOD checkout/success.php, but rather intercept the success.twig before it is displayeid and change it with my own twig with additional info which I'would manage within the event code and the corresponding language files. Notably, after some struggles, I already managed to fix event management occurring at catalog side (viewtopic.php?f=202&t=227497). But now I'm stuck again.. : I create a new event and register it correctly (admin side in admin/controller/extension/module/dflighteventmanager.php):

Code: Select all

public function install()
  {
    $this->model_setting_event->addEvent(
      'module_dflighteventmanager_changetwig',
	  'catalog/view/theme/default/template/common/success/before',
      'extension/module/dflighteventmanager/modify_twig'
    );
}
wherease, following some examples in the thread, here is the code which should be fired by the event (in catalog/controller/extension/module/dflighteventmanager.php):
     public function modify_twig(&$route, &$data)  {
			// $this->session_start('24e32bdb7a143d1c302882f519');
			$this->log->debug("NEW ORDER EVENT MANAGER TRIGGERED! - SUCCESS");
				$this->log->debug('route: '. json_encode($route));
				$this->log->debug('data: ' . json_encode($data));

				$route = str_replace('common/success', 'extension/module/dflighteventmanager', $route);
	} // end function
n.b. I placed a file in catalog/view/theme/default/template/extension/module/dflighteventmanager.twig which is what I expect to be loaded as an output.
But the trigger doesn't seem to be fired. What I'm doing wrong :-?
Thanks for help :-)

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by OSWorX » Fri Mar 04, 2022 12:23 am

@cbaldoni: I would call the event with after.

Code: Select all

public function modify_twig(&$route, &$data, &$output)  {
Assuming you have all your $data, you can assign the new template like

Code: Select all

$output = $this->load->view('YOUR_TEMPLATE_HERE', $data);
If not, get your $data before you get the new template and use that values.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by straightlight » Fri Mar 04, 2022 4:04 am

catalog/view/theme/default/template/common/success/before
That path in the installer is also wrong. Please refer to the install/opencart.sql > oc_event table for more information.

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 OSWorX » Fri Mar 04, 2022 5:03 am

straightlight wrote:
Fri Mar 04, 2022 4:04 am
catalog/view/theme/default/template/common/success/before
That path in the installer is also wrong. Please refer to the install/opencart.sql > oc_event table for more information.
The path itself would be correct - if not used for events.
Should be more like this:

Code: Select all

catalog/view/common/success/after
or /before when he really wants that ..

Using this, it will work with all templates.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by straightlight » Fri Mar 04, 2022 5:49 am

OSWorX wrote:
Fri Mar 04, 2022 5:03 am
straightlight wrote:
Fri Mar 04, 2022 4:04 am
catalog/view/theme/default/template/common/success/before
That path in the installer is also wrong. Please refer to the install/opencart.sql > oc_event table for more information.
The path itself would be correct - if not used for events.
Should be more like this:

Code: Select all

catalog/view/common/success/after
or /before when he really wants that ..

Using this, it will work with all templates.
It's under the addEvent method in this context. Therefore, the user is not using the path correctly.

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 cbaldoni » Fri Mar 04, 2022 4:05 pm

thank you straightlight!! works perfectly both /before and /after. The str_replace to change $route needs to be coded in the /before frame to be effective. Thanks!!!! ;D

Newbie

Posts

Joined
Fri Feb 25, 2022 2:26 am

Post by straightlight » Sat Mar 05, 2022 12:47 am

cbaldoni wrote:
Fri Mar 04, 2022 4:05 pm
thank you straightlight!! works perfectly both /before and /after. The str_replace to change $route needs to be coded in the /before frame to be effective. Thanks!!!! ;D
Yes, with this representation, that would be correct. :)

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
Who is online

Users browsing this forum: No registered users and 3 guests