Post by webdesires » Wed Oct 23, 2024 11:50 pm

I'm having an aweful bad time here trying to use the new opencart 4.0.2.3 events...

Here is my event install code:

Code: Select all

$data = [
            'code' => 'better_invoice',
            'description' => 'Better Invoice Event',
            'trigger' => 'admin/view/sale/order_invoice/before',
            'action' => 'extension/better_invoice/controller/invoice_event.onInvoiceGenerate',
            'sort_order' => 1,
            'status' => 1
        ];

        $this->model_setting_event->addEvent($data);
I have placed the code for this event in /extension/better_invoice/admin/controller/invoice_event.php

the code:

Code: Select all

namespace Opencart\Admin\Controller\Extension\BetterInvoice;

class InvoiceEvent extends \Opencart\System\Engine\Controller {
    public function onInvoiceGenerate(string &$route, array &$data, mixed &$output) {
        $this->load->model('tool/log');
        $this->model_tool_log->write('onInvoiceGenerate event triggered');
        //die('test');
        // Fetch the order ID from the $args
        $order_id = $data[0];

        // Load your custom invoice model here
        //$this->load->model('extension/better_invoice/invoice');
        //$output = $this->model_extension_better_invoice_invoice->generateCustomInvoice($order_id);
        $this->response->setOutput('Custom Invoice Output');
    }
}
I see it install fine, but loading an invoice up from an order just loads normally with no issues, what am I doing wrong?

Could someone also give me a brief rundown on the different events for this? for example I know there is a before/after and right now im doing the event on admin/view/ but think i can also do it on controller, can I replace the invoice twig itself only? how can I add more variables to the controller but retain all other functionality?

Any help will be much appreciated because this has been a whole day battle, and getting nowhere at the moment!
Last edited by webdesires on Fri Oct 25, 2024 2:12 am, edited 1 time in total.

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by JNeuhoff » Thu Oct 24, 2024 12:02 am

If it is a before-event, then the function should start like this:

Code: Select all

public function onInvoiceGenerate( string &$route, array &$data, string &$template_code ): void {
    ....
    return;
}

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


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by webdesires » Thu Oct 24, 2024 12:06 am

still doing nothing.

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by webdesires » Fri Oct 25, 2024 12:20 am

I've fixed the issue and now it is running as intended, my issue now is what is the best practice for replacing the template with my own? it appears I cannot change the process and my only real option is to just call

Code: Select all

$this->response->setOutput($this->load->view(
and then exit; killing any ongoing execution.

Is this how I should approach this or is there another way?

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by OSWorX » Fri Oct 25, 2024 12:38 am

webdesires wrote:
Fri Oct 25, 2024 12:20 am
I've fixed the issue and now it is running as intended, my issue now is what is the best practice for replacing the template with my own? it appears I cannot change the process and my only real option is to just call

Code: Select all

$this->response->setOutput($this->load->view(
and then exit; killing any ongoing execution.

Is this how I should approach this or is there another way?
Unfortunely: yes.
Make it the same way in some extensions, did not found a better way until now.
Or - another possibility - depends if an before or after event - replacing the variable $output (or template or whatever you name it ..) with you own code.

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


User avatar
Administrator

Posts

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

Post by webdesires » Fri Oct 25, 2024 2:12 am

No its ok I found out that simply returning $this->load->view overwrites the output:

Code: Select all

return $this->load->view('extension/better_invoice/event/order_invoice', $data);
This is definitely the best approach.

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by JNeuhoff » Fri Oct 25, 2024 5:11 pm

In a view/before event you have to check whether the &$template_code is actually loaded or not:

Code: Select all

public function onInvoiceGenerate( string &$route, array &$data, string &$template_code ): void {
    if ($template_code==null) {
        // load the unrendered template code from its twig file
        $template_code = ....;
    }

    // modify your $data
    .....

    // modify your $template_code
    .....

    return;
}
It will probably be easier for many cases to use view/after events, where you can directly modify the rendered $output for your needs.

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


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by webdesires » Fri Oct 25, 2024 6:40 pm

Could you specify more detail please? Why would you need to check this, and what does the variable actually contain? is it the source Twig or the rendered twig? what would you do if this is set and why would you worry about it specifically if you are trying to override the entire template?

Thanks

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by JNeuhoff » Fri Oct 25, 2024 6:50 pm

The view/before event uses an un-rendered template code which can be modified in any way you want. Just be careful not to make it too incompatible with other extensions using the same template.

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


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by OSWorX » Sat Oct 26, 2024 4:40 am

webdesires wrote:
Fri Oct 25, 2024 6:40 pm
Could you specify more detail please? Why would you need to check this, and what does the variable actually contain? is it the source Twig or the rendered twig? what would you do if this is set and why would you worry about it specifically if you are trying to override the entire template?

Thanks
As already written above (and asked a few times!): all depends on a before or after event.
Before: has (no) template/output code - this variable will be (nearly always) empty (not set).
After: variable template/output will contain the rendered (twig) template - therefore can be "manipulated" the way you need/want.

But why do you not inspect the already existing code provided since OC 2.x ?
Events are not new, they exist (rudimentar) since 2015 - now we are close to 2025 (10 years after) - but many of you do as they would be so new ..?!

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


User avatar
Administrator

Posts

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

Post by webdesires » Sat Oct 26, 2024 8:33 pm

::) to me they are new.

Refused to even learn how they worked because they were so poorly implemented initially, were buggy, and don't mention the fact that commands and how to use this events system have changed many many times during minor versions as well as major.

Opencart is a mess - its development is poorly handled. We have always used VQ/OCmod up until this point, just like everyone else. Quite a lot of plugin developers just like us are the same as you will see on the marketplace, they still require VQ/OCmod to be installed for their plugins.

So give me a break I was only asking for you to give me some insights, I've decided to try learn and use this event system, and some kindness goes a long way, I now understand what your comments were about, thank you.

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom

Post by webdesires » Sat Oct 26, 2024 8:36 pm

Just to drive my point across....

The OFFICIAL developer documentation of OpenCart: https://docs.opencart.com/en-gb/developer/module/

No mention of events, no sample code, no event lists, in fact it still shows old code paths and talks about OCMOD.

Regards, WebDesires.
We are a team of developers in the UK - professional and friendly, message us or give us a call anytime and we will be happy to help.

Phone: +44 (0) 121 318 6336 - Web: webdesires.co.uk - Skype: WebDesires
OpenCart Support - OpenCart Web Development - Our OpenCart Plugins


User avatar
Active Member

Posts

Joined
Mon Sep 28, 2015 6:34 pm
Location - West Midlands, United Kingdom
Who is online

Users browsing this forum: No registered users and 2 guests