AddEventHandler catalog OnSuccessCatalogImpo rt1C setCatalogElementMin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?
AddEventHandler("catalog", "OnSuccessCatalogImport1C", "setCatalogElementMinPrice");
function setCatalogElementMinPrice() {
if(!(CModule::IncludeModule('iblock') && CModule::IncludeModule('catalog'))){
return;
}
$importIblockId = $_SESSION["BX_CML2_IMPORT"]["NS"]["IBLOCK_ID"];
//закончили импорт торговых предложений
if($importIblockId != SKU_IBLOCK_ID){
return;
}
$output .= date('d.m.Y H:i:s') . ' setCatalogElementMinPrice' . "\n";
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/bitrix/success_1c_import.log', $output, FILE_APPEND);
/*собираем все цены из торговых предложений*/
$arFilter = array(
"IBLOCK_ID" => SKU_IBLOCK_ID,
"ACTIVE" => "Y",
//">CATALOG_QUANTITY" => 0
);
$arSelect = array(
"ID",
"IBLOCK_ID",
"NAME",
"CATALOG_PRICE_1",
"PROPERTY_CML2_LINK"
);
$arElementsPrices = array();
$rsElements = CIBlockElement::GetList(array('CATALOG_PRICE_1'=>'DESC'), $arFilter, false, false, $arSelect);
while( $arElement = $rsElements->GetNext() ) {
$CATALOG_ELEMENT_ID = $arElement['PROPERTY_CML2_LINK_VALUE'];
$arElementsPrices[ $CATALOG_ELEMENT_ID ][] = $arElement['CATALOG_PRICE_1'];
}
/*проставляем их элементам*/
$arFilter = array(
"IBLOCK_ID" => CATALOG_IBLOCK_ID,
"ACTIVE" => "Y"
);
$rsElements = CIBlockElement::GetList(array(), $arFilter);
while( $arElement = $rsElements->GetNext() ) {
$ID = $arElement['ID'];
$minPrice = min($arElementsPrices[ $ID ]);
CIBlockElement::SetPropertyValuesEx($ID, CATALOG_IBLOCK_ID, array("MINIMUM_PRICE" => $minPrice));
}
$GLOBALS['CACHE_MANAGER']->ClearByTag('iblock_id_' . CATALOG_IBLOCK_ID);
$output .= date('d.m.Y H:i:s') . ' setCatalogElementMinPrice end' . "\n";
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/bitrix/success_1c_import.log', $output, FILE_APPEND);
}