Views: 2757
Last Modified: 24.05.2022
You can supplement the standard set of restrictions with your own. To do that, use the restriction initializing events as you see fit:
- for delivery services onSaleDeliveryRestrictionsClassNamesBuildList:
Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'onSaleDeliveryRestrictionsClassNamesBuildList',
'myDeliveryFunction'
);
- for payment systems onSalePaySystemRestrictionsClassNamesBuildList:
Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'onSalePaySystemRestrictionsClassNamesBuildList',
'myPayFunction'
);
- for cash registers onSaleCashboxRestrictionsClassNamesBuildList:
Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'onSaleCashboxRestrictionsClassNamesBuildList',
'myCashboxFunction'
);
- for companies onSaleCompanyRulesClassNamesBuildList:
Bitrix\Main\EventManager::getInstance()->addEventHandler(
'sale',
'onSaleCompanyRulesClassNamesBuildList',
'myCompanyFunction'
);
Accordingly, you need to return your restriction class in the event handlers:
- for delivery services:
function myDeliveryFunction()
{
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS,
array(
'\MyDeliveryRestriction' => '/bitrix/php_interface/include/mydelrestriction.php',
)
);
}
- for payment systems:
function myPayFunction()
{
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS,
array(
'\MyPayRestriction' => '/bitrix/php_interface/include/mypayrestriction.php',
)
);
}
- for cash registers:
function myCashboxFunction()
{
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS,
array(
'\MyCashboxRestriction' => '/bitrix/php_interface/include/mycashboxrestriction.php',
)
);
}
- for companies:
function myCompanyFunction()
{
return new \Bitrix\Main\EventResult(
\Bitrix\Main\EventResult::SUCCESS,
array(
'\MyCompanyRestriction' => '/bitrix/php_interface/include/mycompanyrestriction.php',
)
);
}
Next, by describing the restriction, you can introduce any rules of your choice. A restriction for delivery service availability on lunar days only is introduced in the example below:
use Bitrix\Sale\Delivery\Restrictions;
use Bitrix\Sale\Internals\Entity;
class MyDeliveryRestriction extends Restrictions\Base
{
public static function getClassTitle()
{
return 'on lunar days';
}
public static function getClassDescription()
{
return 'delivery will be performed only within the indicated range of lunar days';
}
public static function check($moonday, array $restrictionParams, $deliveryId = 0)
{
if ($moonday < $restrictionParams['MIN_MOONDAY']
|| $moonday > $restrictionParams['MAX_MOONDAY'])
return false;
return true;
}
protected static function extractParams(Entity $shipment)
{
$json = file_get_contents('http://moon-today.com/api/index.php?get=moonday');
$res = json_decode($json, true);
return !empty($res['moonday']) ? intval($res['moonday']) : 0;
}
public static function getParamsStructure($entityId = 0)
{
return array(
"MIN_MOONDAY" => array(
'TYPE' => 'NUMBER',
'DEFAULT' => "1",
'LABEL' => 'Minimum days'
),
"MAX_MOONDAY" => array(
'TYPE' => 'NUMBER',
'DEFAULT' => "30",
'LABEL' => 'Maximum days'
)
);
}
}