Last active
May 20, 2016 18:02
-
-
Save zualex/bd56da3ae4e02f6d603b1d9856bffe21 to your computer and use it in GitHub Desktop.
Битрикс, события, код
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 | |
/* | |
* Событие - при изменении элемента проверяем значение свойства и меняем в активность элемента | |
*/ | |
/* | |
* Регистрация двух событий: | |
* OnBeforeIBlockElementAdd - перед добавление элемента | |
* OnBeforeIBlockElementAdd - перед изменением элемента | |
*/ | |
AddEventHandler("iblock", "OnBeforeIBlockElementAdd", Array("EventChangeActive", "OnSetActive")); | |
AddEventHandler("iblock", "OnBeforeIBlockElementUpdate", Array("EventChangeActive", "OnSetActive")); | |
class EventChangeActive | |
{ | |
/* | |
* При значении свойства "Отображать на сайте: НЕТ", то деактивируем элемент, и наоборот | |
*/ | |
function OnSetActive(&$arFields) | |
{ | |
foreach($arFields['PROPERTY_VALUES'] as $propId => $prop){ | |
if(self::getCodeValue($propId) === 'OTOBRAZHAT_NA_SAYTE'){ | |
$propertyValue = self::getPropertyValue($propId, $prop[0]['VALUE']); | |
if(count($propertyValue) && $propertyValue['VALUE'] == 'НЕТ'){ | |
$arFields['ACTIVE'] = 'N'; | |
} | |
if(count($propertyValue) && $propertyValue['VALUE'] == 'ДА'){ | |
$arFields['ACTIVE'] = 'Y'; | |
} | |
break; | |
} | |
} | |
} | |
/* | |
* Получение кода свойства | |
*/ | |
function getCodeValue($propId = ''){ | |
if($propId === ''){return false;} | |
$propInfoRes = CIBlockProperty::GetByID($propId); | |
$propInfo = $propInfoRes->GetNext(); | |
return $propInfo['CODE']; | |
} | |
/* | |
* получение значения свойства | |
*/ | |
function getPropertyValue($propId = '', $propIdValue = ''){ | |
if($propId === ''){return false;} | |
if($propIdValue === ''){return false;} | |
$db_enum_list = CIBlockProperty::GetPropertyEnum($propId, array(), array('ID' => $propIdValue)); | |
return $db_enum_list->GetNext(); | |
} | |
} | |
?> |
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 | |
namespace Partner\InformatsionnyeKarty; | |
/* | |
* Событие для синхронизации higloadblock выгруженного из 1С с купонами для скидки | |
*/ | |
/* | |
* Регистрация событий для higloadblock Информационные карты | |
*/ | |
$eventManager = \Bitrix\Main\EventManager::getInstance(); | |
$eventManager->addEventHandler('', 'InformatsionnyeKartyOnAfterUpdate', '\Partner\InformatsionnyeKarty\KartyOnAfterAddUpdate'); | |
$eventManager->addEventHandler('', 'InformatsionnyeKartyOnAfterAdd', '\Partner\InformatsionnyeKarty\KartyOnAfterAddUpdate'); | |
$eventManager->addEventHandler('', 'InformatsionnyeKartyOnAfterDelete', '\Partner\InformatsionnyeKarty\KartyOnAfterAddUpdate'); | |
/* | |
* Получение списка карт и сохранение их как купонов для скидки | |
*/ | |
function KartyOnAfterAddUpdate(\Bitrix\Main\Entity\Event $event){ | |
$cards = array(); | |
$code_field = "UF_KODKARTY"; | |
$id_discount = "5"; | |
$listKarty = \InformatsionnyeKartyTable::getList(); | |
while($arKarty = $listKarty->Fetch()){ | |
$cards[$arKarty[$code_field]] = $arKarty[$code_field]; | |
} | |
if (\CModule::IncludeModule("catalog")){ | |
//Удаление всех купонов | |
$deleteCoupon = \CCatalogDiscountCoupon::DeleteByDiscountID($id_discount); | |
//Добавление купонов | |
foreach($cards as $coupon){ | |
$addCoupon = \CCatalogDiscountCoupon::add(array( | |
"DISCOUNT_ID" => $id_discount, | |
"ONE_TIME" => "N", | |
"COUPON" => $coupon | |
)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment