Post by qusaiqusaitaq » Tue Aug 06, 2024 9:26 pm

"Hello everyone, I am trying to make my extension send order information to my website when a new order is created or its status is updated, but the event is not calling the function for these operations. Here is an example of my controller.

Code: Select all

<?php
namespace Opencart\Admin\Controller\Extension\CanaryExtension\Module;

class CanaryExtension extends \Opencart\System\Engine\Controller {
    public function index(): void {
        // Load the language file for this extension
        $this->load->language('extension/canary_extension/module/canary_extension');

        // Set the document title
        $this->document->setTitle($this->language->get('heading_title'));

        // Load the model for this extension
        $this->load->model('setting/setting');

        // Save the settings if the form is submitted
        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            $this->model_setting_setting->editSetting('module_canary_extension', $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            $this->response->redirect($this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true));
        }

        // Set data for view
        $data['action'] = $this->url->link('extension/canary_extension/module/canary_extension', 'user_token=' . $this->session->data['user_token'], true);
        $data['cancel'] = $this->url->link('extension/module', 'user_token=' . $this->session->data['user_token'], true);
        $data['user_token'] = $this->session->data['user_token'];

        // Retrieve the status of the module
        $data['module_canary_extension_status'] = $this->config->get('module_canary_extension_status');

        // Load the view
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('extension/canary_extension/module/canary_extension', $data));
    }

    public function install(): void {
        $this->load->model('setting/event');
        $this->model_setting_event->addEvent([
            'code'        => 'canary_extension_order_add',
            'description' => 'Canary Extension: Add Order',
            'trigger'     => 'catalog/model/checkout/order/addOrder/after',
            'action'      => 'extension/canary_extension/module/canary_extension/onOrderAdd',
            'status'      => 1,
            'sort_order'  => 1
        ]);
        $this->model_setting_event->addEvent([
            'code'        => 'canary_extension_order_status_update',
            'description' => 'Canary Extension: Update Order Status',
            'trigger'     => 'catalog/model/checkout/order/addHistory/after',
            'action'      => 'extension/canary_extension/module/canary_extension/onOrderStatusUpdate',
            'status'      => 1,
            'sort_order'  => 1
        ]);
    }

    public function uninstall(): void {
        $this->load->model('setting/event');
        $this->model_setting_event->deleteEventByCode('canary_extension_order_add');
        $this->model_setting_event->deleteEventByCode('canary_extension_order_status_update');
    }

    public function onOrderAdd(): void {
        $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "add_order_new` (
            `tf_courier_id` int(11) NOT NULL,
            `name` varchar(255) NOT NULL,
            `status` int(2) NOT NULL,
            PRIMARY KEY (`tf_courier_id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");

        $data = [
            'order' => 1,
            'customer' => 1
        ];
        file_put_contents('order_status_update_log.txt', print_r($data, true) . "\n", FILE_APPEND);
        $this->sendDataToWebsite($data);
    }

    public function onOrderStatusUpdate(): void {
        $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "update_order_new` (
            `tf_courier_id` int(11) NOT NULL,
            `name` varchar(255) NOT NULL,
            `status` int(2) NOT NULL,
            PRIMARY KEY (`tf_courier_id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");

        $data = [
            'order' => 1,
            'customer' => 1,
            'status' => 1
        ];

        file_put_contents('order_status_update_log.txt', print_r($data, true) . "\n", FILE_APPEND);
        $this->sendDataToWebsite($data);
    }

    private function sendDataToWebsite($data) {
        $ch = curl_init('https://bot.taqnyat.sa/webhook_response/webhook.php');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $response = curl_exec($ch);
        curl_close($ch);

        return $response;
    }
}
?>

Newbie

Posts

Joined
Mon Aug 05, 2024 6:53 pm

Post by softmonke » Wed Aug 07, 2024 10:28 am

Hello there! In the future, do wrap your code in [ code ][ /code ] tags (without the spaces) or add it as an attachment.

Anyway, what OpenCart version are you using? Assuming it's 4.0.2.x, when adding your event in your "install" function, the event action should be:

Code: Select all

'action' => 'extension/canary_extension/module/canary_extension.onOrderStatusUpdate',
Same for "onOrderAdd".

That's because in OC4, the query path uses the "." character rather than "/".

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by qusaiqusaitaq » Thu Aug 08, 2024 2:28 pm

I've tried it in different ways:
'action' => 'extension/canary_extension/module/canary_extension.onOrderStatusUpdate',
'action' => 'extension/canary_extension/module/canary_extension|onOrderStatusUpdate',
'action' => 'extension/canary_extension/module/canary_extension/onOrderStatusUpdate',

but is still same

Newbie

Posts

Joined
Mon Aug 05, 2024 6:53 pm

Post by JNeuhoff » Thu Aug 08, 2024 8:08 pm

I can't see anything wrong with your code, as long as it uses the dot-separator, e.g. 'extension/canary_extension/module/canary_extension.onOrderStatusUpdate'. I assume you are using OpenCart 4.0.2.3.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am

Who is online

Users browsing this forum: No registered users and 2 guests