Проблема обновления цены через событие OnBeforePriceUpdate. Если сохраняешь товар через форму, то она обновляется как надо, но необходимо чтобы работало при выгрузке. Есть статья с обратной ситуацией, а как быть в моей?
Код
AddEventHandler("catalog", "OnBeforePriceUpdate", Array("UpdateProduct", "newPrice"));
class UpdateProduct{
protected static $handlerDisallow = 0;
public static function disableHandler()
{
self::$handlerDisallow--;
}
public static function enableHandler()
{
self::$handlerDisallow++;
}
public static function isEnabledHandler()
{
return (self::$handlerDisallow >= 0);
}
public static function newPrice($ID, &$arFields){
/* проверяем, что обработчик уже запущен */
if (!self::isEnabledHandler())
return;
/* взводим флаг запуска */
self::disableHandler();
\Bitrix\Main\Loader::includeModule("catalog");
$allProductPrices = \Bitrix\Catalog\PriceTable::getList([
"select" => [
"ID",
"PRICE",
"CATALOG_GROUP_ID"
],
"filter" => [
"PRODUCT_ID" => $arFields["PRODUCT_ID"],
]
])->fetchAll();
$addProps = CIBlockElement::GetList(
Array(),
Array("ID" => $arFields["PRODUCT_ID"]),
false,
false,
Array(
'ID', "NAME", "PROPERTY_NO_UPDATE_PRICE", "PURCHASING_PRICE"
)
);
if($ar_res = $addProps->GetNext()){
foreach($allProductPrices as $key => $value){
if($allProductPrices[$key]["CATALOG_GROUP_ID"] == 1){
$arPrice["BASE_ID"] = $allProductPrices[$key]["ID"];
$arPrice["BASE_PRICE"] = $allProductPrices[$key]["PRICE"];
} elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 2){
$arPrice["MERLION_PRICE"] = $allProductPrices[$key]["PRICE"];
} elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 3){
$arPrice["MONITORING_PRICE"] = $allProductPrices[$key]["PRICE"];
} elseif($allProductPrices[$key]["CATALOG_GROUP_ID"] == 4){
$arPrice["TEHNO_PRICE"] = $allProductPrices[$key]["PRICE"];
}
}
$newPrice["PRODUCT_ID"] = $arFields["PRODUCT_ID"];
$newPrice["CURRENCY"] = "RUB";
$newPrice["CATALOG_GROUP_ID "] = 1;
if($ar_res["PROPERTY_NO_UPDATE_PRICE_VALUE"] == "Y"){
if(isset($arPrice["TEHNO_PRICE"]) and $arPrice["TEHNO_PRICE"] != 0){
$newPrice["PRICE"] = $arPrice["TEHNO_PRICE"];
}
} else{
if(isset($arPrice["MONITORING_PRICE"]) and $arPrice["MONITORING_PRICE"] != 0){
if(((($arPrice["MONITORING_PRICE"] - $ar_res["PURCHASING_PRICE"]) * 100) / $arPrice["MONITORING_PRICE"]) <= -5){
$newPrice["PRICE"] = 0;
$update = new CIBlockElement();
$res2 = $update->Update($ar_res["ID"], array("ACTIVE" => "N"));
if ($res2->isSuccess()){
AddMessage2Log(date("d.m.Y H:i:s") . " | Закупочная цена меньше розничной на 5% и более процентов - ".$arFields["PRODUCT_ID"], "");
}
} else{
$newPrice["PRICE"] = $arPrice["MONITORING_PRICE"];
}
} elseif(isset($arPrice["TEHNO_PRICE"]) and $arPrice["TEHNO_PRICE"] != 0){
$newPrice["PRICE"] = $arPrice["TEHNO_PRICE"];
} elseif(isset($arPrice["MERLION_PRICE"]) and $arPrice["MERLION_PRICE"] != 0){
$newPrice["PRICE"] = $arPrice["MERLION_PRICE"];
}
}
if($arPrice["BASE_PRICE"] != $newPrice["PRICE"] and !empty($newPrice["PRICE"])){
$res = \Bitrix\Catalog\Model\Price::update($arPrice["BASE_ID"], $newPrice);
if ($res->isSuccess()){
AddMessage2Log(date("d.m.Y H:i:s") . " | Изменение цены на ".$newPrice["PRICE"]." для ".$ar_res["NAME"]." ".$arFields["PRODUCT_ID"], "");
}
}
/* вновь разрешаем запускать обработчик */
self::enableHandler();
}
}
}
$res = \Bitrix\Catalog\Model\Price::update($arPrice["BASE_ID"], $newPrice); if ($res->isSuccess()){ AddMessage2Log(date("d.m.Y H:i:s") . " | Изменение цены на ".$newPrice["PRICE"]." для ".$ar_res["NAME"]." ".$arFields["PRODUCT_ID"], ""); }
А вот это зачем, когда вам нужно изменить данные в $arFields ?
Не надо сверлить зубы через задний проход дрелью от Сваровски