July 19, 2017

debug opencart 2.3 events triggers not working

Opencart's Event system is a better alternative to vQmod, although how to use it is not obvious, and the documentation is poorly written. As usual, working with Opencart is a major pain in the ass. I've spent quite a lot of hours trying to figure out how Events are supposed to work. I decided to write something down because surprisingly there doesn't seem to be a lot of information online. If you found this and still can't figure it out, please comment and I'll try to elaborate.

Here's how I was debugging them:

  • To debug what events are present on a given page, go to the Event::trigger() function and var_dump the $event variable. Those are the hooks you can listen on
  • Use either catalog or admin (depending on where's your callback) controller/startup/event.php file to check if your event was registered. This file's register function adds the events to the queue using the similarly named register() function from system/engine/event.php above. Once it's done, you can dump it's $this->data variable and check if a. your event is there and b. if it has a valid action. Here's what a valid action looks like:
  • If your action is null, the system may be looking for a wrong callback path. The action for your event is built upon the callback route you provide when you register an event. That route is then converted into a full path of a file the system tries to load and execute. 
  • If you're trying to hook before admin/controller/catalog/product::index(), your event trigger will be admin/controller/catalog/product/before, note that "index" gets omitted.
  • Another gotcha. When Opencart tries to execute an action, it checks if the number of parameter your callback action expects matches the number of arguments it's about to pass to it. If it doesn't match, the action will fail silently, with the Error: Could not call message only visible in the debug trace. The solution is to make your callback arguments optional.

No comments: