В этой статье мы используем модуль Подарочный сертификат, доступный в маркетплейсе 1С Битрикс
Подарочный сертификат
- Установите модуль Подарочный сертификат и настройте его.
- Перейдите в файловую систему /bitrix/php_interface/ и откройте или создайте файл init.php
- Добавьте скрипт в файл init.php:
class IsaevCertificatedCustomMaxPrice {
public static function onDiscountUpdate(\Bitrix\Main\ORM\Event $event)
{
$fields = $event->getParameter('fields');
$primary = $event->getParameter('id');
if ($fields['PRESET_ID'] !== 'Isaev\Certificates\Discount\Preset\CertificatePreset') {
return;
}
if (!isset($fields['APPLICATION'])) {
$fullObject = \Bitrix\Sale\Internals\DiscountTable::getByPrimary($primary['ID'])->fetch();
$applicationFn = $fullObject['APPLICATION'];
} else {
$fields = $event->getParameter('fields');
$applicationFn = $fields['APPLICATION'];
}
$result = new \Bitrix\Main\Entity\EventResult(\Bitrix\Main\Entity\EventResult::SUCCESS);
$modifyFields = [];
$modifyFields['APPLICATION'] = str_replace(
'\Bitrix\Sale\Discount\Actions::applyToBasket($arOrder, ["VALUE" => -100, "UNIT" => "P"], "");',
'\Bitrix\Sale\Discount\Actions::applyToBasket($arOrder, ["VALUE" => 1.0, "UNIT" => "C", "LIMIT_VALUE" => 0], "");',
$applicationFn
);
$result->modifyFields($modifyFields);
return $result;
}
public static function onDiscountAdd(\Bitrix\Main\ORM\Event $event)
{
$tempObject = $event->getParameter('object');
if ($tempObject->get('PRESET_ID') !== 'Isaev\Certificates\Discount\Preset\CertificatePreset') {
return;
}
$applicationFn = $tempObject->get('APPLICATION');
$discountId = $tempObject->get('ID');
$object = \Bitrix\Sale\Internals\DiscountTable::getByPrimary($discountId)->fetchObject();
$object->set('APPLICATION', str_replace(
'\Bitrix\Sale\Discount\Actions::applyToBasket($arOrder, ["VALUE" => -100, "UNIT" => "P"], "");',
'\Bitrix\Sale\Discount\Actions::applyToBasket($arOrder, ["VALUE" => 1.0, "UNIT" => "C", "LIMIT_VALUE" => 0], "");',
$applicationFn
));
$object->save();
}
}
$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler("sale", "DiscountOnAfterAdd", [IsaevCertificatedCustomMaxPrice::class, "onDiscountAdd"]);
$eventManager->addEventHandler("sale", "DiscountOnBeforeUpdate", [IsaevCertificatedCustomMaxPrice::class, "onDiscountUpdate"]);
- Пересохраните сертификаты.