Рецепты и примеры кода для CMS Bitrix

Проставить галку для определения наличия товара в регионе на примере аспро


$eventManager->addEventHandler(
	'aspro.max',
	'OnAsproRegionalityAddSelectFieldsAndProps',
	function(&$arSelect){
		$arSelect[] = 'PROPERTY_REGION_AVAILIBLE_CODE';
	}
);
$eventManager->addEventHandler(
	'catalog',
	'OnStoreProductAdd',
	'OnStoreProductUpdateHandler'
);
$eventManager->addEventHandler(
	'catalog',
	'OnStoreProductUpdate',
	'OnStoreProductUpdateHandler'
);
function OnStoreProductUpdateHandler($ID, $arFields, $del = false){
	if(!empty($arFields['STORE_ID']) && !empty($arFields['PRODUCT_ID'])){
		$iRegionIBlockID = CMaxCache::$arIBlocks['s1']['aspro_max_regionality']['aspro_max_regions'][0];
		$catalogId = \Bitrix\Main\Config\Option::get('aspro.max', 'CATALOG_IBLOCK_ID', CMaxCache::$arIBlocks['s1']['aspro_max_catalog']['aspro_max_catalog'][0]);
		$element = \Bitrix\Iblock\Elements\ElementCatalogTable::getByPrimary($arFields['PRODUCT_ID'], [
			'select' => ['ID', 'IBLOCK_ID'],
		])->fetch();
		if(!($element && $element['IBLOCK_ID'] == $catalogId))return;
		$dbRes = \CIBLockElement::GetList([], ['ACTIVE' => 'Y', 'IBLOCK_ID' => $iRegionIBlockID, 'PROPERTY_STORES_LINK' => $arFields['STORE_ID']], false, false, ['ID', 'NAME', 'IBLOCK_ID', 'PROPERTY_STORES_LINK', 'PROPERTY_REGION_AVALIBLE_CODE']);
		$counter = 0;
		$arRegion = [];
		while($obRegion = $dbRes->GetNextElement()){
			$arRegion[$counter] = $obRegion->GetFields();
			$arRegion[$counter] = $obRegion->GetProperties();
			$REGION_AVAILIBLE_CODE = $arRegion[$counter]['REGION_AVAILIBLE_CODE']['VALUE'];
			$REGION_STORES = $arRegion[$counter]['STORES_LINK']['VALUE'];
			$REGION_AVALIBLE_VALUE = null;
			$propertyEnums = \CIBlockPropertyEnum::GetList(["DEF"=>"DESC", "SORT"=>"ASC"], ["IBLOCK_ID" => $catalogId, "CODE" => $REGION_AVAILIBLE_CODE, "XML_ID" => 'Y']);
			while($enumFields = $propertyEnums->fetch()){
				$REGION_AVALIBLE_VALUE = $enumFields['ID'];
			}
			if(!$REGION_AVALIBLE_VALUE){
				file_put_contents(__DIR__ . "/error_availible.txt", var_export([date("d.m.Y h:i:s"), $enumFields], true), FILE_APPEND);
			}
			$avalible = false;
			foreach($REGION_STORES as $storeId){
				if($del && $storeId == $arFields['STORE_ID']) continue;
				if($storeId == $arFields['STORE_ID']){
					if($arFields['AMOUNT'] > 0){
						$avalible = true;
						break;
					}
					continue;
				}
				$rsStoreProduct = \Bitrix\Catalog\StoreProductTable::getList([
					'filter' => ['ID' => $storeId, 'PRODUCT_ID' => $arFields['PRODUCT_ID']],
					'select' => ['PRODUCT_ID', 'AMOUNT','STORE_ID'],
				]);
				while($arStoreProduct = $rsStoreProduct->fetch()){
					if($arStoreProduct['AMOUNT'] > 0){
						$avalible = true;
						break;
					}
				}
			}
			$value = $avalible?$REGION_AVALIBLE_VALUE:false;
			CIblockElement::SetPropertyValuesEx($arFields['PRODUCT_ID'], $catalogId, [$REGION_AVAILIBLE_CODE => $value]);
			$counter++;
		}
	}
}
$eventManager->addEventHandler(
	'catalog',
	'OnBeforeStoreProductDelete',
	'OnBeforeStoreProductDeleteHandler'
);
function OnBeforeStoreProductDeleteHandler($ID){
	$rsStoreProduct = \Bitrix\Catalog\StoreProductTable::getList([
		'filter' => ['ID'=>$ID],
		'select' => ['PRODUCT_ID', 'AMOUNT','STORE_ID'],
	]);
	while($arStoreProduct = $rsStoreProduct->fetch()){
		OnStoreProductUpdateHandler($ID, $arStoreProduct, true);
	}
}
	

\Bitrix\Main\Loader::includeModule('iblock');
\Bitrix\Main\Loader::includeModule('aspro.max');
\Bitrix\Main\Loader::includeModule('catalog');

$resAll = \Bitrix\Iblock\Elements\ElementCatalogTable::getList([
		'select' => ['ID', 'IBLOCK_ID'],
		'filter' => ['IBLOCK_ID' => 119],
])->fetchAll();
foreach($resAll as $res){
	OnStoreProductUpdateHandler(null, ['PRODUCT_ID' => $res['ID'], 'AMOUNT' => 0, 'STORE_ID' => 22]);
}