Created
April 26, 2016 10:42
-
-
Save s2ar/888daab643d070355cc000d9372ca891 to your computer and use it in GitHub Desktop.
Bitrix. Обновление базовой цены минимальной ценой предложения
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
AddEventHandler("catalog", "OnPriceUpdate", array("MyClassUpdatePrice", "OnBeforePriceUpdateHandler")); | |
AddEventHandler("catalog", "OnPriceAdd", array("MyClassUpdatePrice", "OnBeforePriceUpdateHandler")); | |
AddEventHandler("iblock", "OnBeforeIBlockElementDelete", Array("MyClassUpdatePrice", "OnBeforeIBlockElementDeleteHandler")); | |
class MyClassUpdatePrice | |
{ | |
function OnBeforePriceUpdateHandler($PRICE_ID, $arFields) { | |
if(!$arFields['PRICE']) $arFields['PRICE'] = 0; | |
if(!$arFields['PRODUCT_ID']) $arFields['PRODUCT_ID'] = $PRICE_ID; | |
if($arFields['PRODUCT_ID']){ | |
MyClassUpdatePrice::updatePrice($arFields['PRODUCT_ID']); | |
} | |
} | |
function OnBeforeIBlockElementDeleteHandler($id) { | |
MyClassUpdatePrice::updatePrice($id,false); | |
} | |
/** | |
* Обновляет цену товара | |
* @param $PRODUCT_ID - id предложения | |
* @param $takeCurr - учитывать или нет цену текущего элемента | |
*/ | |
function updatePrice($PRODUCT_ID, $takeCurr = true){ | |
if($PRODUCT_ID){ | |
$rProp = CIBlockElement::GetProperty(4, $PRODUCT_ID, "sort", "asc", array("CODE" => "CML2_LINK")); | |
if ($aProp = $rProp->GetNext()){ | |
$aFilter = Array("IBLOCK_ID"=>4, "ACTIVE"=>"Y", 'PROPERTY_CML2_LINK'=>$aProp['VALUE']); | |
$rPart = CIBlockElement::GetList(Array('sort'=>'ASC'), $aFilter, false, false, Array()); | |
$minPrice = 0; | |
while($oPart = $rPart->GetNextElement()): | |
$aPart = $oPart->GetFields(); | |
if(!$takeCurr && $aPart['ID']== $PRODUCT_ID ) continue; | |
$aPrice = CPrice::GetBasePrice($aPart['ID']); | |
if($aPrice["PRICE"]<$minPrice || $minPrice==0){ | |
$minPrice = $aPrice["PRICE"]; | |
} | |
endwhile; | |
// Минимальную цену найдено. Обновим ее у товара | |
// Обновляем цену | |
$arFieldsPrice = Array( | |
"PRODUCT_ID" => $aProp['VALUE'], | |
"CATALOG_GROUP_ID" => 1, | |
"PRICE" => $minPrice, | |
"CURRENCY" => "RUR" | |
); | |
$resPrice = CPrice::GetList( | |
array(), | |
array( | |
"PRODUCT_ID" => $aProp['VALUE'], | |
"CATALOG_GROUP_ID" => 1 | |
) | |
); | |
if ($arrPrice = $resPrice->Fetch()) { | |
CPrice::Update($arrPrice["ID"], $arFieldsPrice); | |
} else { | |
CPrice::Add($arFieldsPrice); | |
} | |
} | |
} | |
/* | |
$arEventFields = array( | |
'NAME'=>$PRODUCT_ID, | |
'LOGIN'=>$minPrice); | |
CEvent::SendImmediate("NEW_USER", "s1", $arEventFields); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment