A
A
Anatoly Teorsky2020-02-07 09:37:22
1C-Bitrix
Anatoly Teorsky, 2020-02-07 09:37:22

Why is the event handler not being called?

Good afternoon.

The event handler registered in its module in the ../install/index.php file is not called

RegisterModuleDependences('sale', 'OnSaleOrderSaved', $this->MODULE_ID, '\\Yolo\\Bonus\\EventHandler', 'myFunction');

everything is OK here, he registered, I already did this with others)

Now in the file ../lib/Eventhandler.php
<?php
namespace Yolo\Bonus;
require_once($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');

$module_id = "yolo.bonus";
\CModule::IncludeModule('main');
\CModule::IncludeModule('sale');
\CModule::IncludeModule($module_id);

use Bitrix\Main;
use Bitrix\Main\Loader;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\Application;
use Bitrix\Main\Type\DateTime;
use Yolo\Bonus;
use Yolo\Bonus\Core;
use Bitrix\Sale;
use Bitrix\Sale\Order;
use Bitrix\Sale\Basket;
use Bitrix\Sale\Delivery;
use Bitrix\Sale\PaySystem;
class EventHandler{
  public function OnSaleStatusOrder($order_id, $status_val){
  // ...	
  }
  
  public function myFunction(\Bitrix\Main\Event $event)
{
    /** @var Order $order */
    $order = $event->getParameter("ENTITY");
    $oldValues = $event->getParameter("VALUES");
    $isNew = $event->getParameter("IS_NEW");

    if ($isNew)
    {
        $sum = $order->getPrice();
        // . . . 
    }
}
}
?>

As a result, an error occurs:
Argument 1 passed to Yolo\Bonus\EventHandler::myFunction() must be an instance of Bitrix\Main\Event, instance of Bitrix\Sale\Order given (0)

As I understand it, the Bitrix\Sale\Order object is passed to the function and not Bitrix\Main\Event.

I have already connected everything that is possible, and still I don’t understand what’s wrong.
In fact, I just started writing the module and maybe I still don’t understand a lot of things, but everything seemed to be logical up to this point.
The documentation shows that Bitrix\Main\Event should be transmitted, but in fact Bitrix\Sale\Order is transmitted...

Please tell me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PetrPo, 2020-02-07
@mamkaror

I had to blunt a little ...
If you register an event like this, your version will work ok

$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->registerEventHandler('sale', 'OnSaleOrderSaved', $this->MODULE_ID, '\\Yolo\\Bonus\\EventHandler', 'myFunction');

5e3d6ad0da8ad239350040.jpeg
PS do not forget to delete your event from the database and clear the PPS cache
in the docks on the account of the \Bitrix\Main\Event $event object, everything is written correctly, just mixing the old and new api turned out)) The same garbage will turn out between AddEventHandler and $eventManager->addEventHandler

A
Andrey Nikolaev, 2020-02-07
@gromdron

First, such methods should be marked as static so as not to cause errors in php itself.
Secondly, there is a description of events - https://dev.1c-bitrix.ru/api_d7/bitrix/sale/events... which says that the event handler takes 3 parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question