Post by opencart_wandi » Mon Feb 12, 2024 7:59 pm

Hello community,


Basically I am developing an extension for OC 4.0.2.3 and I am encountering an issue. I want the function createShipmentWandiAfter to be triggered after a new order is added but i can't make it work. Once I install the extension, the event is correctly created (see picture at the end) but in any case it triggers when I complete a purchase in the checkout.

I have even added some additional logging to the addOrder function to debug that is properly working and that is the data that I need in my event, but can't manage to trigger it. Any help would be extremely appreciated! :)

-
Here the code:

Code: Select all

    public function install(): void {
        $this->load->model('setting/event');
        $this->model_setting_event->deleteEventByCode('enable_dark_theme');
        $this->model_setting_event->deleteEventByCode('post_shipment_to_wandi');



        $this->model_setting_event->addEvent([
            'code'          => 'post_shipment_to_wandi', 
            'description'   => '',
            'trigger'       => 'catalog/model/checkout/order/addHistory/after',
            'action'        => $this->event . '.createShipmentWandiAfter',
            'status'        => true,
            'sort_order'    => 1
        ]);

        $this->model_setting_event->addEvent([
            'code'          => 'enable_dark_theme', 
            'description'   => '',
            'trigger'       => 'admin/controller/common/header/before',
            'action'        => $this->event . '.runDarkThemeScripts',
            'status'        => true,
            'sort_order'    => 1
        ]);





        $this->load->model('user/user_group');
        $groups = $this->model_user_user_group->getUserGroups();

        foreach($groups as $group) {
            $this->model_user_user_group->addPermission($group['user_group_id'], 'access', $this->path);
            $this->model_user_user_group->addPermission($group['user_group_id'], 'access', $this->event);
        }
    }

    public function uninstall(): void {
        if ($this->user->hasPermission('modify', $this->path)) {
            $this->load->model('setting/event');
            $this->model_setting_event->deleteEventByCode('enable_dark_theme');
            $this->model_setting_event->deleteEventByCode('post_shipment_to_wandi');
        }
    }

    public function runDarkThemeScripts($route, &$args): void {
        if ($this->config->get($this->module . '_status')) {
            $this->document->addScript('../extension/wandioc/admin/view/javascript/darktheme.js');
            $this->document->addStyle('../extension/wandioc/admin/view/styles/darktheme.css');
        }
    }

    public function createShipmentWandiAfter(&$route, &$args , &$output): void {
        $this->log->write('Purchase Event Catched'.print_r($args), TRUE);
        $this->log->write('Route: ' . $route);
        $this->log->write('Order Info: ');
        $this->log->write(print_r($args));
    }

    public function createShipmentWandiBefore(&$route, &$args): void {
        $this->log->write('Purchase Event Catched'.print_r($args), TRUE);
        $this->log->write('Route: ' . $route);
        $this->log->write('Order Info: ');
        $this->log->write(print_r($args));
    }

}

event created and active:
Image


Posts

Joined
Sun Feb 11, 2024 1:28 am

Post by halfhope » Tue Feb 13, 2024 1:20 am


My FREE extensions in marketplace. [ security | flexibility | speedup ]


User avatar
Active Member

Posts

Joined
Tue Dec 10, 2013 9:44 pm
Location - San Diego

Post by opencart_wandi » Wed Feb 14, 2024 11:23 pm

halfhope wrote:
Tue Feb 13, 2024 1:20 am
Hi!

Maybe this can help you https://opencartforum.com/topic/182390- ... nt-1790429
Appreciate the help but was not the case.
Leaving here the full code in case any angel can help:

File tree:

Code: Select all

wandioc
└── extension
    └── wandioc
        ├── admin
        │   ├── controller
        │   │   └── module
        │   │       ├── post_shipment.php
        │   │       └── wandioc.php
        │   ├── language
        │   │   └── en-gb
        │   │       └── module
        │   │           └── wandioc.php
        │   └── view
        │       └── template
        │           └── module
        │               └── wandioc.twig
        └── catalog
            └── controller
                └── wandioc.php
                

extension/wandioc/admin/controller/module/wandioc.php

Code: Select all

<?php
namespace Opencart\Admin\Controller\Extension\Wandioc\Module;
class Wandioc extends \Opencart\System\Engine\Controller {

    private $module = 'wandioc';
    private $path = 'extension/wandioc/module/wandioc';
    private $event = 'extension/wandioc/module/wandioc';
    

// This method handles the main index page of the module.
    public function index(): void {
        // Loads language and model resources.
        $this->load->language($this->path);
        $this->load->model('setting/extension');

        // Sets the page title.
        $this->document->setTitle(strip_tags($this->language->get('heading_title')));

        // Builds breadcrumbs for navigation.
        $data['breadcrumbs'] = [];

        // Adds home breadcrumb.
        $data['breadcrumbs'][] = [
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
        ];

        // Adds extension breadcrumb.
        $data['breadcrumbs'][] = [
            'text' => $this->language->get('text_extension'),
            'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module')
        ];

        // Adds module breadcrumb.
        $data['breadcrumbs'][] = [
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link($this->path, 'user_token=' . $this->session->data['user_token'])
        ];

        // Sets URLs for save and back actions.
        $data['save'] = $this->url->link($this->path.'.save', 'user_token=' . $this->session->data['user_token']);
        $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module');

        // Retrieves module status.
        $data['module_name'] = $this->module;
        $data['module_status'] = $this->config->get($this->module . '_status');
        // Retrieve the API key from the settings
        $data['wandi_api_key'] = $this->config->get($this->module . '_wandi_api_key');
        // $this->log->write(print_r($data['wandi_api_key'], TRUE));


        // Loads header, left column, and footer components.
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        // Renders the module view.
        $this->response->setOutput($this->load->view($this->path, $data));
    }

// This method handles saving module settings.
    public function save(): void {
        // Loads language and initializes JSON response.
        $this->load->language($this->path);
        $json = [];

        // Checks user permission.
        if (!$this->user->hasPermission('modify', $this->path)) {
            $json['error'] = $this->language->get('error_permission');
        }

        // If no errors, saves settings.
        if (!$json) {
            $this->load->model('setting/setting');
            
            // Save the input value from the second file (total_voucher_sort_order) as api_key
            $this->log->write(print_r($this->request->post, TRUE));

            // Save all settings including the api_key
            $this->model_setting_setting->editSetting($this->module, $this->request->post);
            //$this->log->write(print_r($this->model_setting_setting, TRUE));

            $json['success'] = $this->language->get('text_success');
            
        }


        // Sends JSON response.
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function install(): void {
        $this->load->model('setting/event');
        $this->model_setting_event->deleteEventByCode('enable_dark_theme');
        $this->model_setting_event->deleteEventByCode('post_shipment_to_wandi');



        $this->model_setting_event->addEvent([
            'code'          => 'post_shipment_to_wandi', 
            'description'   => '',
            'trigger'       => 'catalog/model/checkout/order/addHistory/before',
            'action'        => $this->event . '.createShipmentWandiBefore',
            'status'        => true,
            'sort_order'    => 1
        ]);

        $this->model_setting_event->addEvent([
            'code'          => 'enable_dark_theme', 
            'description'   => '',
            'trigger'       => 'admin/controller/common/header/before',
            'action'        => $this->event . '.runDarkThemeScripts',
            'status'        => true,
            'sort_order'    => 1
        ]);


        $this->load->model('user/user_group');
        $groups = $this->model_user_user_group->getUserGroups();

        foreach($groups as $group) {
            $this->model_user_user_group->addPermission($group['user_group_id'], 'access', $this->path);
            $this->model_user_user_group->addPermission($group['user_group_id'], 'access', $this->event);
        }
    }

    public function uninstall(): void {
        if ($this->user->hasPermission('modify', $this->path)) {
            $this->load->model('setting/event');
            $this->model_setting_event->deleteEventByCode('enable_dark_theme');
            $this->model_setting_event->deleteEventByCode('post_shipment_to_wandi');
        }
    }

    public function runDarkThemeScripts($route, &$args): void {
        if ($this->config->get($this->module . '_status')) {
            $this->document->addScript('../extension/wandioc/admin/view/javascript/darktheme.js');
            $this->document->addStyle('../extension/wandioc/admin/view/styles/darktheme.css');
        }
    }

}
extension/wandioc/catalog/controller/wandioc.php

Code: Select all

<?php
namespace Opencart\Catalog\Controller\Extension\Wandioc\Module;
class Wandioc extends \Opencart\System\Engine\Controller {
private $module = 'wandioc';
private $path = 'extension/wandioc/module/wandioc';
private $event = 'extension/wandioc/module/wandioc';



public function createShipmentWandiBefore(&$route, &$args): void {
    $this->log->write('Purchase Event Catched'.print_r($args), TRUE);
    $this->log->write('Route: ' . $route);
}
}


Posts

Joined
Sun Feb 11, 2024 1:28 am

Post by grgr » Thu Feb 15, 2024 8:14 am

Looking at the image of the event and given this:

Code: Select all

namespace Opencart\Catalog\Controller\Extension\Wandioc\Module;
class Wandioc extends \Opencart\System\Engine\Controller {
Then the action path would appear to be completely incorrect.

-
Image Image Image
VIEW ALL EXTENSIONS * EXTENSION SUPPORT * WEBSITE * CUSTOM REQUESTS


User avatar
Active Member

Posts

Joined
Mon Mar 28, 2011 4:08 pm
Location - UK

Post by soscodes » Sat Apr 27, 2024 5:46 pm

Hi, I tried opencart 4 events but its not calling function of my class. My file name is soscodes_custom.php and I created it under "extension/soscodes/admin/controller/event/soscodes_custom.php"

Code: Select all

namespace Opencart\Admin\Controller\Extension\Soscodes\Event;

class SoscodesCustom extends \Opencart\System\Engine\Controller {

    public function product_data_form_after(string &$route, array &$data, mixed &$output): void {  
echo "here" exit;
}
}

I have inserted event entry directly to event table of database.
Trigger: admin/view/catalog/product_form/after
Action: extension/soscodes/event/soscodes_custom.product_data_form_after

Any suggestion or any step I missed?

Newbie

Posts

Joined
Fri Sep 20, 2019 6:50 pm
Who is online

Users browsing this forum: No registered users and 3 guests