V
V
Vladimir2020-08-10 13:28:29
1C-Bitrix
Vladimir, 2020-08-10 13:28:29

Saving value to order property via API?

Good afternoon, tell me why the save may not work, I tried this:

use Bitrix\Main; 
Main\EventManager::getInstance()->addEventHandler(
    'sale',
   'OnSaleOrderSaved',
   'myFunction'
);

function myFunction(Main\Event $event) {
   $order = $event->getParameter("ENTITY");
   $res = CSaleBasket::GetList(array(), array("ORDER_ID" => $order->getField("ID")));
   
   $items = array();
   
   while ($arItem = $res->Fetch()) { 
      $items .= $arItem['PRODUCT_XML_ID'].':'.$arItem['PRICE_TYPE_ID'].';';
   }
   
   $order->setField("PRICE_TYPE", $items);

   AddMessage2Log($items);

}


so

use Bitrix\Main; 
Main\EventManager::getInstance()->addEventHandler(
    'sale',
   'OnSaleOrderSaved',
   'myFunction'
);

function myFunction(Main\Event $event) {
   $order = $event->getParameter("ENTITY");
   $res = CSaleBasket::GetList(array(), array("ORDER_ID" => $order->getField("ID")));
   
   $items = array();
   
   while ($arItem = $res->Fetch()) { 
      $items .= $arItem['PRICE_TYPE_ID'].';';
   }
   
    $propertyCollection = $order->getPropertyCollection();
        
    $propsData = [];

    foreach ($propertyCollection as $propertyItem) {
        if (!empty($propertyItem->getField("CODE"))) {
            $propsData[$propertyItem->getField("CODE")] = trim($propertyItem->getValue());
        }
    }

    foreach ($propertyCollection as $propertyItem) {

        switch ($propertyItem->getField("CODE")) {
            case 'PRICE_TYPE':
               AddMessage2Log('Условие сработало');
                $propertyItem->setField("VALUE", $items);
                break;
        }
    }
   
   AddMessage2Log($items);

}


The event itself is executed, the order is created, but the value in the field I need is not filled.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-08-10
@gangstarcj

OnSaleOrderSaved - when the order and all related entities have already been saved.
You need to use OnSaleOrderBeforeSaved
https://dev.1c-bitrix.ru/api_d7/bitrix/sale/events...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question