Answer the question
In order to leave comments, you need to log in
How to write a value to a custom field when the order status changes?
Hello!
It is necessary to take the order amount and write the value in the user field (USER volume) when changing the order status. To do this, I add the following code to init.php:
\Bitrix\Main\EventManager::getInstance()->addEventHandler('sale', 'OnSaleStatusOrderChange', ['Handler', 'OnSaleStatusOrderChange']);
class Handler {
function OnSaleStatusOrderChange($event)
{
$parameters = $event->getParameters();
if ($parameters['VALUE'] === 'P')
{
/** @var \Bitrix\Sale\Order $order */
$order = $parameters['ENTITY'];
/*
здесь как то нужно записать значение в поле
*/
}
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS
);
}
}
Answer the question
In order to leave comments, you need to log in
Maybe it will be useful for someone, I solved it like this:
\Bitrix\Main\EventManager::getInstance()->addEventHandler('sale', 'OnSaleStatusOrderChange', ['Handler', 'OnSaleStatusOrderChange']);
class Handler {
function OnSaleStatusOrderChange($event)
{
$parameters = $event->getParameters();
if ($parameters['VALUE'] === 'P')
{
/** @var \Bitrix\Sale\Order $order */
$order = $parameters['ENTITY'];
$userID = $order->getField("USER_ID"); // получаем ID пользователя
$orderPrice = $order->getField("PRICE"); // получаем сумму заказа
//получаем пользовательское поле для записи
global $USER;
$filter = Array("ID" => $userID, "ACTIVE"=> "Y");
$arParams["SELECT"] = array( "UF_BONUS_SUM");
$rsUsers = CUser::GetList(($by="personal_phone"), ($order="desc"), $filter, $arParams);
while ($arUser = $rsUsers->Fetch()) {
$oldSum = $arUser["UF_BONUS_SUM"];
}
$bonusSum = $oldSum + $orderPrice; // сумма заказа + сумма, которая уже записана в пользовательское поле
$user = new CUser;
$fields = Array(
"UF_BONUS_SUM" => $bonusSum,
);
$user->Update($userID, $fields); // обновляем поле
//Bitrix\Main\Diag\Debug::writeToFile(array($order ),"","/log_new.txt");
}
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS
);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question