S
S
smoln2016-07-13 12:26:27
1C-Bitrix
smoln, 2016-07-13 12:26:27

Synchronization of balances in the warehouse of the trade offer and goods, how to do?

Good afternoon, there is an automatic unloading from 1C to Bitrix, but only trade offers are unloaded, respectively, the balances in the warehouses are unloaded, respectively, the balances in the product do not appear, it costs 0 everywhere.
Here is the handler code that will allow you to synchronize the balances. That is, at the time of editing the quantity in stock for a trade offer, we calculate all the balances for other offers and save them for the product.

AddEventHandler("catalog", "OnStoreProductUpdate","MyOnStoreProductSave");
AddEventHandler("catalog", "OnStoreProductAdd","MyOnStoreProductSave");
define("LOG_FILENAME", $_SERVER["DOCUMENT_ROOT"]."/log.txt");
function MyOnStoreProductSave($id, $arFields){   
   $allAmount = $arFields['AMOUNT'];
   if(CModule::IncludeModule('catalog') & CModule::IncludeModule('iblock')){
      //Получаем информацию о товаре привязанном к данному приложения, вмести с этим проверяем не является ли это товаром.      
      $arProductInfo = CCatalogSKU::GetProductInfo($arFields['PRODUCT_ID']);            
      if(is_array($arProductInfo)){
         //Получаем данные о инфоблоке Торговых предложений
         $arOffersInfo = CCatalogSKU::GetInfoByProductIBlock($arProductInfo['IBLOCK_ID']);         
         $arFilter = Array(
            'IBLOCK_ID'=>$arOffersInfo['IBLOCK_ID'],
            "PROPERTY_".$arOffersInfo['SKU_PROPERTY_ID']=>$arProductInfo['ID'],
            "!ID"=>$arFields['PRODUCT_ID']
         );
         //Получаем список торговых предложений.
         $obOffersList = CIBlockElement::GetList(Array("SORT"=>"ASC"),$arFilter,false,false,array());
         $arOffers = array();
         while($arOffers = $obOffersList->Fetch()){
            //Получаем данные об остатках на складе для торгового предложения.
            $obStoreOffer = CCatalogStoreProduct::GetList(array(), array('STORE_ID'=>$arFields['STORE_ID'], 'PRODUCT_ID' =>$arOffers['ID']), false,false,array());
            while($arStore = $obStoreOffer->Fetch()){
               $arAllStore[] = $arStore;
               $allAmount = $allAmount + $arStore['AMOUNT']; 
            }               
         }   
         //Получаем данные о складе товара и формируем массив для обновления данных.
         $obStoreProduct = CCatalogStoreProduct::GetList(array(), array('STORE_ID'=>$arFields['STORE_ID'], 'PRODUCT_ID' =>$arProductInfo['ID']), false,false,array());         
         $arFieldsProduct = array (
             'PRODUCT_ID' => $arProductInfo['ID'],
             'STORE_ID' => $arFields['STORE_ID'],
             'AMOUNT' => $allAmount,
           );
       $result = FALSE;
         if($arStoreProduct = $obStoreProduct->Fetch()){            
           $result = CCatalogStoreProduct::Update($arStoreProduct["ID"],$arFieldsProduct);                  
         }else{
           $result = CCatalogStoreProduct::Add($arFieldsProduct);         
         }       
       if($result){          
          $obStoreProduct = CCatalogStoreProduct::GetList(array(), array('PRODUCT_ID' =>$arProductInfo['ID']), false,false,array());
            while($arStoreProduct = $obStoreProduct->Fetch()){
               $allAmountProduct = $allAmountProduct + $arStoreProduct['AMOUNT'];         
             }                     
          $arFieldsProduct = array(
            "QUANTITY" => $allAmountProduct,
         ); 
          CCatalogProduct::Update ($arProductInfo['ID'],$arFieldsProduct);
       }
      }   
   }   
}

where do I need to push this code so that the balances of the goods are automatically saved, I don’t understand!

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