2

I'm working on my custom payment method for opencart. What I need is to listen to order history changes which is changed from admin by manager. It was all ok, but I couldn't make the opencart event trigger work, no matter what I do.

Here is what it looks like right now:

public function install() {
    $this->load->model('extension/event');

    $this->model_extension_event->addEvent('delayed_payment_oh_add', 'catalog/model/checkout/order/addOrderHistory/after', 'admin/controller/extension/payment/delayed_payment/send_instructions');
    $this->model_extension_event->addEvent('delayed_payment_oh_api', 'catalog/controller/api/order/history/after', 'admin/controller/extension/payment/delayed_payment/send_instructions');
}

public function uninstall() {
    $this->load->model('extension/event');

    $this->model_extension_event->deleteEvent('delayed_payment_oh_add');
    $this->model_extension_event->deleteEvent('delayed_payment_oh_api');
}

And listener itself:

public function send_instructions($route, $output, $order_id, $order_status_id) {
    $this->load->model('checkout/order');

    $this->log->write(var_dump($output));
    echo var_dump($output);

    $order_info = $this->model_checkout_order->getOrder($order_id);

    if ($order_status_id == $this->config->get('delayed_payment_confirmed_order_status'))
        $this->model_checkout_order->addOrderHistory(
            $order_id,
            $this->config->get('delayed_payment_waiting_order_status'),
            $this->config->get('delayed_payment_order_comment'),
            true
        );
}

It is not printing anything to both log or output. How can make it work? (Opencart 2.3)

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Adam Starlight
  • 302
  • 3
  • 14
  • Where is the file that contains send_instructions function? on which directory? – DigitCart Sep 30 '17 at 12:53
  • @Mojtaba I tried it both in both admin (admin/controller/extension/payment/delayed_payment.php) and catalog controllers (catalog/controller/extension/payment/delayed_payment.php) – Adam Starlight Oct 01 '17 at 09:45

1 Answers1

3

I FINALLY MADE IT! This is how to debug your events in Opencart 2.3.

First of all, take a look at file 'system/engine/event.php'. We need to log what exact events are called, to do this change the trigger function like this. It will log all events and actions with their results to file 'system/storage/logs/events.log'

public function trigger($event, array $args = array()) {
    // echo 'Event fired: '.var_dump($event);
    $log = new Log('events.log');
    $log->write('Event fired: '.$event);

    foreach ($this->data as $trigger => $actions) {
        if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($trigger, '/')) . '/', $event)) {
            foreach ($actions as $action) {
                $result = $action->execute($this->registry, $args);
                $log->write('Action executed: '.$action->getId());
                $log->write('Action result: '.$result);

                if (!is_null($result) && !($result instanceof Exception)) {
                    return $result;
                }
            }
        }
    }
}

The first thing i saw was that event name is ok, but i could not read the action path (route). So I changed my install methods:

public function install() {
    $this->load->model('extension/event');

    $this->model_extension_event->addEvent('delayed_payment_oh_add', 'catalog/model/checkout/order/addOrderHistory/after', 'extension/payment/delayed_payment/send_instructions');
}

I changed path and removed line with 'api/order/history/after' its not needed to achieve what I need anyway...

Then the most interesting part comes up If you go to total/voucher event callback you will see that method declaration is:

public function send($route, $output, $order_id, $order_status_id)

It's completly wrong dont use it as an example! If you enable it, in your log will see an exception like 'could not call...'. Its because event methods recieve 3 arguments which all should be references. And now my function looks like this (for my payment method I mean):

public function send_instructions(&$route, &$data, &$output)

$route is a name route called. $data is an array of event arguments (0 => order_ir, 2 => order_state_id e t.c.) You can remove $output for after methods if you want, its usually is NULL. Personally I removed it...

And here you are! Everything works fine. I hope it helped someone :) Don't forget to remove logs in event.php...

Adam Starlight
  • 302
  • 3
  • 14
  • "$output" parameter is not always NULL. In case of event being called after the function, it contains whatever value that function returns. – Nauman Feb 11 '19 at 13:25