Opencart version: 4.0.2.0
PHP version: 8.2
Extension logic: I'm working on an extension that calls to my client's inventory API after an order is placed.
Problem: I can install my extension, but I can't trigger the event.
Documentation used: https://github.com/opencart/opencart/wiki/Events-System, sample OC extensions, misleading chatGPT results.
Glad to report that I was able to create/install an extension easily enough. However, when I check out in my store, I do not see the logs I am printing from the top of my extension. I suspect an issue with namespacing, as I've been having trouble parsing the documentation / example code.
Speaking of code, here is (an abbreviated version of) mine. First, the file structure:
Code: Select all
/public_html
/extension
/iocart_update_inventory
/admin
/controller/module
iocart_update_inventory.php
/language/en-gb/module
iocart_update_inventory.php
/view/template/module
iocart_update_inventory.twig
/catalog
/controller
/iocart_update_inventory
update_inventory.php
Okay, on to some more code. Here's an abbreviated version of my admin controller:
Code: Select all
<?php
namespace Opencart\Admin\Controller\Extension\IocartUpdateInventory\Module;
class iocartupdateinventory extends \Opencart\System\Engine\Controller {
public function index(): void {
// lots of boring language stuff here
...
$this->response->setOutput($this->load->view('extension/iocart_update_inventory/module/iocartupdateinventory', $data));
}
public function save(): void {
// eh, same here because my extension doesn't really have any settings yet.
...
$this->response->setOutput(json_encode($json));
}
public function install(): void {
$this->load->model('setting/event');
// Here's the good part. Notice I'm using the dot notation instead of the pipe because OC version 4.0.2.0.
// but not sure if I got the action path correct. I've tried all kinds of variations of casing, directory structure, and underscores/no underscores in the extension name.
$this->model_setting_event->addEvent([
'code' => 'iocart_update_inventory',
'description' => 'Update inventory in IOCart when an order is placed',
'trigger' => 'catalog/model/checkout/order/addOrder/after',
'action' => 'extension/iocartupdateinventory/iocartupdateinventory.updateInventory'
]);
}
public function uninstall(): void {
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('iocart_update_inventory');
}
}
Okay last one I promise (because there's no need to include the language or twig files). Here's the catalog controller where the magic should happen:
Code: Select all
<?php
namespace Opencart\Catalog\Controller\Extension\iocartupdateinventory\updateinventory;
class iocartupdateinventory extends \Opencart\System\Engine\Controller {
private function getAccessToken() {
// you'll just have to trust me that there's some access token logic here... removed for brevity.
return $token_data['access_token'];
}
public function updateInventory(&$route, &$args, &$output) {
$this->log->write('TEST TEST WHY U NO LOG NOTHING EVER');
try {
// Get OAuth token
$access_token = $this->getAccessToken();
...
// $this->log->write('Successfully updated inventory for order ');
} catch (\Exception $e) {
// Log error
$this->log->write('Failed to update inventory: ' . $e->getMessage());
}
}
}
Please advise on how I should proceed with updating my module naming and file structure. Thanks! - JJ