1

I am new to Opencart modules development and trying to create a custom module with events on OC 2.3.x. Here is the code I did:

class ControllerExtensionModuleFSElias extends Controller
{
    public function install()
    {
        $this->load->model('extension/event');
        $this->model_extension_event->addEvent('fselias', 'admin/model/catalog/product/addProduct/after', 'fselias/events/add_product');
    }

    public function add_product($route, $product_id)
    {
        $this->load->model('sale/order');
        $this->load->model('catalog/product');

        //$product_id - you can get the product id that was recentlt added in this function
        $log = date("F j, Y, g:i a") . ': ' . $route . ': ' . $product_id . PHP_EOL;
        file_put_contents('./products_log_' . date("j.n.Y") . '.txt', $log, FILE_APPEND);
    }
}

The event is being registered yet not getting triggered when adding a new product, please what I have done wrong in there?

Fady Elias
  • 23
  • 1
  • 7

1 Answers1

0

I'm not confident that the string you have provided for argument 3 (fselias/events/add_product) is correct; it's supposed to be a route to a controller. But what you could do to debug this is set a breakpoint in system/engine/loader.php where the post events are triggered and see what's happening with your event. If the event can't be found, the system fails silently - this happened to me, and running the system in the debugger was the only way I could track it down.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • Could you please elaborate on how does one "*see what's happening with your event*"? – ᴍᴇʜᴏᴠ Jul 18 '17 at 15:16
  • If you set a breakpoint in the loader, you'll see how events are processed. For example, perhaps your event doesn't get there, or maybe it does and has some sort of error. – Scott C Wilson Jul 18 '17 at 15:23