Views: 2522
Last Modified: 24.05.2022

You can supplement a standard set of company rules with your own rules. To do it, you need to use the event onSaleCompanyRulesClassNamesBuildList:

Bitrix\Main\EventManager::getInstance()->addEventHandler(
        "sale",
        "onSaleCompanyRulesClassNamesBuildList", 
        "myCompanyRulesFunction"
);

You need to return your class of rules in the event handler:

function myCompanyRulesFunction()
{
    return new \Bitrix\Main\EventResult(
        \Bitrix\Main\EventResult::SUCCESS,
        array(
            '\MyCompanyRules' => '/bitrix/php_interface/include/mycompanyrules.php',
        )
    );
}

By describing the rule itself, you can define your own conditions. For example, the rule for auto-assigning a company depending on lunar days is provided in the example below:

use Bitrix\Sale\Services\Base;
use Bitrix\Sale\Internals\Entity;

class MyCompanyRules extends Base\Restriction
{
    public static function getClassTitle()
    {
        return 'by lunar days';
    }

    public static function getClassDescription()
    {
        return 'company will be used only in the specified range of lunar days';
    }

public static function check($params, array $restrictionParams, $serviceId = 0)
{
    if ($params < $restrictionParams['MIN_MOONDAY']
        || $params > $restrictionParams['MAX_MOONDAY'])
        return false;

    return true;
}
protected static function extractParams(Entity $entity)
{
    $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'
            )
        );
    }
}



Courses developed by Bitrix24