C
C
countZer02021-12-22 19:06:39
1C-Bitrix
countZer0, 2021-12-22 19:06:39

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
        );
      }
    }


Field type: string. The field itself belongs to the user. Help me to understand

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kudis, 2021-12-22
@kudis

$order->setFields($arFieldsToChange);

C
countZer0, 2021-12-23
@countZer0

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 question

Ask a Question

731 491 924 answers to any question