Exclusion from indexing of products that are not in stock in 1C Bitrix

Welcome. There is such a problem in bitrix - there is a search for titles search.title. It is necessary that it does not index products whose sales offers are not available in warehouses. I wrote the code on the test page, I enter the product ID manually - everything shows correctly, whether there is a TP and everything seems to be fine. Well, then in init.php I call the event before indexing and try to exclude unnecessary products in the same way, but they still somehow pass. What could be the problem a problem?

AddEventHandler("search", "BeforeIndex", "BeforeIndexHandler");
function BeforeIndexHandler($arFields)
{
CModule::IncludeModule('iblock');
CModule::IncludeModule('catalog');      
$TovarExist = false;
$TypeProduct = CCatalogSKU::getOffersList($arFields['ID']);
if($TypeProduct != false){ 
    foreach($TypeProduct as $key => $ProdSku){              
        foreach($ProdSku as $key => $SkuID){                
            for ($i = 2; $i < 12; $i++) {
                if ($i !== 8 && $i !== 9) { // неактивные склады
                $rsStore = CCatalogStoreProduct::GetList(array(), array('PRODUCT_ID' =>$SkuID['ID'], 'STORE_ID' => $i), false, false, array());
                $arStore = $rsStore->Fetch();
                    if($arStore['AMOUNT'] > 0){
                        $TovarExist = true;
                    }
                }
            }                            
        }
    }       

}
if($TovarExist === false){ 
        $arFields["BODY"] = $arFields["TITLE"] = '';
        unset($arFields["BODY"]); //на всякий случай
        unset($arFields["TITLE"]);
}

}

Author: Grekov Ilya, 2016-08-01

2 answers

First: the handler must pass &$arFields. Secondly, you run through the loop setting the flag, and check it when it falls out of the loop, that is, you only work with the last value of the flag. For some reason, it seems to me that it should be like this:

...    
if ($arStore['AMOUNT'] < 0) { 
    $arFields['BODY'] = $arFields['TITLE'] = ''; 
    unset($arFields['BODY'], $arFields['TITLE']);
}
...

You can pass any number of arguments to unset. and we reduce the number of processed conditions. For processing warehouses, you can also make a simplification. Get the hell out of for. Create an array with warehouses. You can even form it with using the API, selecting only active warehouses CCatalogStore::GetList(array(), array("ACTIVE" => "Y"), array('ID'), or something like that, here is the dock . And check through in_array(). if works faster than for. The fewer cycles, the faster the script runs.

 1
Author: Nikolaj Sarry, 2016-08-02 07:22:26

The problem was solved by adding $arFields["TAGS"] = '';

 0
Author: Grekov Ilya, 2016-08-06 11:29:50