Created
December 11, 2021 08:06
-
-
Save Isa3v/7fda1289d991e6f1ee45f4a753db32fe 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 | |
$eventManager = \Bitrix\Main\EventManager::getInstance(); | |
// Local\Events\Restrictions - Класс через композер | |
$eventManager->addEventHandler('sale', 'onSalePaySystemRestrictionsClassNamesBuildList', ['\Local\Events\Restrictions', 'onCheckRulePayment']); |
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 | |
use Bitrix\Sale\Services\Base; | |
use Bitrix\Sale\Internals\CollectableEntity; | |
use Bitrix\Sale\Internals\Entity; | |
use Bitrix\Sale\Order; | |
/** | |
* Класс для проверки на присутствии свойства (список) в одном из товаров в корзине | |
*/ | |
class CheckCreditType extends Base\Restriction | |
{ | |
public const PROPERTY_CODE = 'SBER_CREDIT_AVAILABLE'; | |
public const PROPERTY_IBLOCK = '20'; | |
public static function getClassTitle() | |
{ | |
return 'По кредиту'; | |
} | |
public static function getClassDescription() | |
{ | |
return 'платежная система не будет выводится, если у товаров установлено свойство'; | |
} | |
/** | |
* Проведем проверку | |
* @param $arBasket | |
* @param array $restrictionParams | |
* @param int $serviceId | |
* @return bool | |
* @throws \Bitrix\Main\SystemException | |
*/ | |
public static function check($arBasket, array $restrictionParams, $serviceId = 0) | |
{ | |
if (!empty($restrictionParams)) { | |
foreach ($arBasket as $basketItem) { | |
// Если есть запрещенные свойства | |
if (in_array($basketItem['PROPERTY'][self::PROPERTY_CODE]['VALUE_ENUM_ID'], $restrictionParams[self::PROPERTY_CODE])) { | |
return false; | |
// Если выбрано пустое | |
} elseif (empty($basketItem['PROPERTY'][self::PROPERTY_CODE]['VALUE_ENUM_ID']) && in_array('null', $restrictionParams[self::PROPERTY_CODE])) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
/** | |
* Вернем корзину для проверки | |
* @param Entity $entity | |
* @return array|bool|mixed | |
* @throws \Bitrix\Main\LoaderException | |
*/ | |
protected static function extractParams(Entity $entity) | |
{ | |
if ($entity instanceof CollectableEntity) { | |
$collection = $entity->getCollection(); | |
$order = $collection->getOrder(); | |
} elseif ($entity instanceof Order) { | |
$order = $entity; | |
} | |
if (!$order->getBasket()) { | |
return false; | |
} | |
$basket = $order->getBasket(); | |
$basketCollection = $basket->getBasketItems(); | |
$returnBasket = array(); | |
if (count($basketCollection) > 0) { | |
foreach ($basketCollection as $basketItem) { | |
// Все свойства получаем для товара | |
$arProperty = \CIBlockElement::GetByID($basketItem->getField('PRODUCT_ID'))->GetNextElement()->GetProperties(); | |
$returnBasket[$basketItem->getField('PRODUCT_ID')]['PROPERTY'] = $arProperty; | |
} | |
} | |
return $returnBasket; | |
} | |
public static function getPropertiesList(): array | |
{ | |
$propertiesList = []; | |
$propertiesList['null'] = 'Не заполнено'; // Не заполнено | |
$propertiesEnum = \Bitrix\Iblock\PropertyEnumerationTable::query() | |
->where('PROPERTY.IBLOCK_ID', self::PROPERTY_IBLOCK) | |
->where('PROPERTY.CODE', self::PROPERTY_CODE) | |
->addSelect('ID') | |
->addSelect('VALUE') | |
->fetchCollection(); | |
if ($propertiesEnum && $propertiesEnum->count()) { | |
foreach ($propertiesEnum as $enum) { | |
$propertiesList[$enum->getId()] = $enum->getValue(); | |
} | |
} | |
return $propertiesList; | |
} | |
/** | |
* Вывод в админке | |
* @param int $entityId | |
* @return array | |
* @throws \Bitrix\Main\LoaderException | |
* @throws \Bitrix\Main\SystemException | |
*/ | |
public static function getParamsStructure($entityId = 0): array | |
{ | |
return [ | |
self::PROPERTY_CODE => [ | |
"TYPE" => "ENUM", | |
'MULTIPLE' => 'Y', | |
"OPTIONS" => self::getPropertiesList(), | |
"LABEL" => 'Запретить, если у одного из товаров выбрано', | |
] | |
]; | |
} | |
} |
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 Local\Events; | |
class Restrictions | |
{ | |
/** | |
* Ограничения для платежных систем | |
* @return \Bitrix\Main\EventResult | |
*/ | |
public function onCheckRulePayment() | |
{ | |
// Нужно указать от корня файл с классом | |
$filePayments = '/local/classes/Events/Restrictions/Payments.php'; | |
return new \Bitrix\Main\EventResult( | |
\Bitrix\Main\EventResult::SUCCESS, | |
[ | |
// Название класса в файле | |
'\CheckCreditType' => $filePayments, // по типу кредита | |
] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment