A
A
Alexander2020-01-31 15:02:32
1C-Bitrix
Alexander, 2020-01-31 15:02:32

How can I update the product price after the OnAfterIBlockElementUpdate event?

The task when changing one of the properties of a product in the admin panel (this property is the cost of the product in currency), you automatically need to update the price of the product (recalculate the price from the Price property in currency and save it in the price).

As I understand it, for this you need to run the OnAfterIBlockElementUpdate event handler for the information block. Below is my code

AddEventHandler('iblock', 'OnAfterIBlockElementUpdate', array("IblockHandler", "UpdateElement"));

class IblockHandler {

    // создаем обработчик события "OnAfterIBlockElementUpdate"
    public function UpdateElement(&$arFields){
        setLog($arFields['ID']);
        self::_updateField($arFields);
    }  

    static private function _updateField($arFields) {
      
        CModule::IncludeModule("catalog");
        $arField = Array(
            "PRODUCT_ID" => $arFields['ID'],
            "CATALOG_GROUP_ID" => 1,
            "PRICE" => '2', // тестовое значение цены товара
            "CURRENCY" => "RUB"
        );
        $res = CPrice::GetList(array(),array("PRODUCT_ID" => $arFields['ID'], "CATALOG_GROUP_ID" => 1));
        if ($arr = $res->Fetch()) {
            CPrice::Update($arr["ID"], $arField);
        } 
        else {
            CPrice::Add($arField);
        }
    }

}


For some reason, the code that should update the price of the product does not work on the OnAfterIBlockElementUpdate event. Although if you run it in the PHP Bitrix console, the price is updated, what could be the reason?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question