P
P
po4emu4ka20202020-10-10 08:13:39
1C-Bitrix
po4emu4ka2020, 2020-10-10 08:13:39

How to change the coefficient of unit of measure for a product in the OnAfterIBlockElementUpdate event handler?

I'm trying to change the unit factor in the OnAfterIBlockElementUpdate event handler .
But the value remains the same .

AddEventHandler("iblock", "OnAfterIBlockElementUpdate", "HandlerAfterIBlockElementUpdate");
function HandlerAfterIBlockElementUpdate(&$arFields)
{
 //новый коэффициент
    $newCoeff = 40;//например

    CModule::IncludeModule("iblock");
    CModule::IncludeModule("catalog");
    CModule::IncludeModule("sale");

    $iBlockId = 4;//ИБ

//получаем коэффициент этого товара для изменения
    $curElementRatio = CCatalogMeasureRatio::getList(
        Array(),
        array('IBLOCK_ID' => $iBlockId, 'PRODUCT_ID' => $arFields['ID']),
        false, false);

    while ($arRatio = $curElementRatio->GetNext()) {
        $ratioId = $arRatio['ID'];
        $arFieldsNew = Array(
            'PRODUCT_ID' => $arFields['ID'],
            'RATIO' => $newCoeff,
        );
//изменяем кэффициент
        CCatalogMeasureRatio::update($ratioId, $arFieldsNew);
    }
}


If the code is run not in an event handler, but simply as a script, then the coefficient changes normally.

I assume that the coefficient is overwritten after the handler is triggered.
Can you please tell me how to solve this problem?

I would be grateful for any advice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
po4emu4ka2020, 2020-10-11
@po4emu4ka2020

As I expected, the result of the action of the OnAfterIBlockElementUpdate event handler is overwritten.
As a result, the OnPriceUpdate price change event handler became the solution (since this event fires at the very end).

AddEventHandler("catalog", "OnPriceUpdate", "HandlerOnPriceUpdate");
function HandlerOnPriceUpdate($ID,&$arFields)
{
    $element = $arFields['PRODUCT_ID'];    //ID товара
    $iblockId = 4;
    $coeff= 100; //например
        $curElementRatio = CCatalogMeasureRatio::getList(
        Array(),
        array('IBLOCK_ID' => $iblockId, 'PRODUCT_ID' => $element ),
        false, false);

    while ($arRatio = $curElementRatio->GetNext()) {
        $ratioId = $arRatio['ID'];
        $arFieldsNew = Array(
            'RATIO' => $coeff,
        );

        CCatalogMeasureRatio::update($ratioId, $arFieldsNew);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question