A
A
Alexander2020-09-23 13:30:18
1C-Bitrix
Alexander, 2020-09-23 13:30:18

Why does the OnSaleOrderSaved event fire multiple times?

I made a handler for the OnSaleOrderSaved event and noticed that this event fires for me three times after placing an order, although it should probably work only once.

The code of my event handler OnSaleOrderSaved

use Bitrix\Main;
use Bitrix\Main\EventManager;
use Bitrix\Main\Loader;
use Bitrix\Main\Event;
use Bitrix\Sale\Order;
use Bitrix\Main\Entity;
global $USER;

$eventManager = EventManager::getInstance();


// ЗАКАЗ
// при создании заказа
$eventManager->addEventHandler(
    "sale", 
    "OnSaleOrderSaved", 
    "onSaleOrderSaved"
);

function onSaleOrderSaved(Bitrix\Main\Event $event){
  $order = $event->getParameter("ENTITY");
  setLog('OnSaleOrderSaved'); // записываем в файл что обработчик был вызван
}

// запись в файл логов
function setLog($arFields){
    file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log_iblock_setlog.txt', serialize($arFields)."\r\n", FILE_APPEND);
}


When I place an order, the setLog function is called three times and three records are created in the file, although only one should be created. What could be the reason? Or is this how it should be? OnSaleOrderSaved always fires at least three times?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
ggrachdev, 2020-09-23
@inviziblll

Perhaps this is due to the fact that the order has several statuses. So you created an order - there is one status, the payment went through - it becomes a different status (i.e. you need to resave it), the status is set - the payment is completed - the automation of processes is triggered and the status is set completed - once again resaving is in progress

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question