B
B
Bogdan2020-04-27 16:30:12
1C-Bitrix
Bogdan, 2020-04-27 16:30:12

Why are the stock balances frayed (CCatalogStoreProduct)?

Good afternoon. I need to have duplicate warehouses with the same balances. To synchronize leftovers, I try to use the OnStoreProductUpdate event, which works fine if I change a duplicate warehouse with a larger id. But if you change the warehouse that was created earlier, then, judging by the debug results, first the result I need is written to the duplicate warehouse, and then it is replaced by the old result (as far as I understand, the remains are simply updated from smaller to larger id). I bind warehouses via UF_ID_MAIN. Tell me how to solve this problem, maybe there are other, more optimal ways to duplicate warehouses?

AddEventHandler("catalog", "OnStoreProductUpdate","MyOnStoreProductSave");
function MyOnStoreProductSave($id, $arFields)
{
    $arFilter = Array("ACTIVE" => "Y", "ID"=>$arFields["STORE_ID"]);

    $rsStores = CCatalogStore::GetList(
        array(),
        $arFilter,
        false,
        false,
        array("ID", "UF_ID_MAIN")
    );

    while($arStore = $rsStores->Fetch()) {
        $arStores[] = $arStore;
    }
   
    $rs = CCatalogStoreProduct::GetList(false, array('PRODUCT_ID'=> $arFields["PRODUCT_ID"], 'STORE_ID' => $arStores[0]['UF_ID_MAIN']));
    while($ar_fields = $rs->GetNext())
    {
        if($ar_fields['AMOUNT'] != $arFields['AMOUNT']){
        $arFieldsNew = Array(
            "PRODUCT_ID" => $arFields["PRODUCT_ID"],
            "STORE_ID" => $arStores[0]['UF_ID_MAIN'],
            "AMOUNT" =>  $arFields['AMOUNT'],
        );
         CCatalogStoreProduct::Update($ar_fields['ID'], $arFieldsNew);
           }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-04-27
@rpsv

The question is why keep duplicates. If to display some information, then it is easier to transfer duplicate warehouses to IB.
-
In order not to lose data, in the handler, BEFORE changing the balances, save the old values ​​​​(to a table or somewhere else), and AFTER the update, change the values ​​​​as you need (so as not to be tied to the processing order).
-
If there is no event after processing, then start an agent that synchronizes the balances in warehouses (by default, the agent is inactive). As soon as the balance update event occurs, activate the agent in the event. After the agent starts, synchronizes the remnants, and after execution, it deactivates itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question