Documentation

applyRestriction

public static function
\Bitrix\Sale\Services\Base\RestrictionManager::applyRestriction(
	int $serviceId,
	RestrictionInfo $restrictionInfo
);

This method applies specified restriction for a service. Static method.

Parameters

ParameterDescriptionVersion
$serviceIdService ID.
$restrictionInfoWrapper for restriction. Contains type of restriction and array containing options for this restriction.

Examples

 
public static function applyRestriction(int $serviceId, RestrictionInfo $restrictionInfo): Result
{
	$result = new Result();

	$restriction = static::getRestriction($restrictionInfo->getType());

	$reflectionClass = new \ReflectionClass(static::class);
	$methodPath = $reflectionClass->getName() . "::applyRestriction";

	if (!$restriction)
	{
		Logger::addError("[{$methodPath}] restriction '{$restrictionInfo->getType()}' not found.");

		$publicErrorMessage = Loc::getMessage('SALE_BASE_RSTR_MANAGER_FIND_RSTR_ERROR', [
			'#RSTR_CLASSNAME#' => htmlspecialcharsbx($restrictionInfo->getType()),
		]);
		$result->addError(new \Bitrix\Main\Error($publicErrorMessage));

		return $result;
	}

	/**
	*    @var \Bitrix\Main\Entity\Result $restrictionApplyResult
	*    @var Restriction $restriction
	*/
	$restrictionApplyResult = $restriction::save([
		'SERVICE_ID' => $serviceId,
		'SERVICE_TYPE' => static::getServiceType(),
		'PARAMS' => $restrictionInfo->getOptions(),
	);

	if (!$restrictionApplyResult->isSuccess())
	{
		foreach ($restrictionApplyResult->getErrors() as $error)
		{
			Logger::addError("[{$methodPath}] " . $error->getMessage());
		}

		$publicErrorMessage = $restriction::getOnApplyErrorMessage();
		$result->addError(new Error($publicErrorMessage));
	}

	return $result;
}


© «Bitrix24», 2001-2024
Up